Class Tm1Session

java.lang.Object
io.skipcheck.tm1.client.session.Tm1Session

public final class Tm1Session extends Object
One TM1 session, shared by every service. Thread-safe, and safe to share between any number of concurrent callers: TM1 runs requests on one session in parallel.
  • Login is lazy and happens once. The first request logs in, and concurrent first requests share that login. Only the login carries the credentials; every other request carries just the session cookie, since a request with credentials and no cookie makes TM1 open another session.
  • Close it. close() ends the TM1 session. Otherwise it stays open on the server, counting against its sessions, until it times out.
  • Any request. execute(Tm1Request) and stream(Tm1Request) send any Tm1Request, so anything the services don't cover yet is still reachable.

Build a session with CAM security and a label:

// CAM security, with a label that shows in Sessions and Threads
return Tm1Session.builder()
	.address("https://tm1.example.com:8010")
	.authenticator(Tm1Authenticator.camNamespace("alice", password, "LDAP"))
	.sessionContext("Sales planning")
	.build();

Or log in with a CAM passport, such as one from exchanging an OIDC token with Cognos:

// CAM passports expire, so the session asks for a current one on every login
return Tm1Session.builder()
	.address("https://tm1.example.com:8010")
	.authenticator(Tm1Authenticator.camPassport(cognos))
	.build();

Send a request no service covers yet, here streaming a collection:

// GET MessageLogEntries, which has no service yet: the latest 20 entries
return session.stream(Tm1Request.list(ODataPath.of("MessageLogEntries"),
		ODataQuery.none().orderByDescending(MessageLogEntry.TIME_STAMP).top(20), MessageLogEntry.class));

Run a service's requests in a sandbox by giving it a sandbox view of the session:

// The cell: every dimension, in the cube's order
Map<String, String> cell = new LinkedHashMap<>();
cell.put("Region", "North");
cell.put("Month", "Jan");
return new SandboxService(session).create(SandboxPatch.builder().name("What-if").build())
	.then(new CellService(session.withSandbox("What-if")).update("Sales", cell, 1200));
  • Method Details

    • builder

      public static Tm1Session.Builder builder()
      A builder for a session on one TM1 server.
      Returns:
      a new builder
    • execute

      public <T> reactor.core.publisher.Mono<T> execute(Tm1Request<T> request)
      Sends a request and reads its response: the entity or value for Tm1Request.Response.SINGLE, the text for Tm1Request.Response.TEXT, or nothing for Tm1Request.Response.NONE. Logs in first if needed.

      TM1's errors arrive as Tm1ApiException, failures below HTTP as Tm1TransportException.

      Type Parameters:
      T - the type the response is read as
      Parameters:
      request - the request to send; not a collection request
      Returns:
      the response, empty for Tm1Request.Response.NONE, or an IllegalArgumentException for a collection request
    • stream

      public <T> reactor.core.publisher.Flux<T> stream(Tm1Request<T> request)
      Sends a Tm1Request.Response.COLLECTION request and streams its items as they're decoded, never holding the whole response. Follows @odata.nextLink if TM1 pages. A Tm1Request.Response.BYTES request streams the body's buffers as they arrive.
      Type Parameters:
      T - the type of each item
      Parameters:
      request - the collection request to send
      Returns:
      the items, or an IllegalArgumentException for a request that isn't a collection request
    • version

      public reactor.core.publisher.Mono<Tm1Version> version()
      The server's product version, such as 11.8.03500.6, read from Server.ProductVersion once per login. Logs in first if needed.
      Returns:
      the version
    • capabilities

      public reactor.core.publisher.Mono<Tm1Capabilities> capabilities()
      What the server supports, from its own $metadata. Fetched once per server version and shared by every session built from the same settings, a pool's included. A request that declares Tm1Request.requires is checked against it before it's sent.
      Returns:
      the capabilities
    • withSandbox

      public Tm1Session withSandbox(String name)
      A view of this session whose requests run in sandbox name: each gets TM1's !sandbox query option, unless the request sets its own. The view shares this session's login, cookie and connections, so it costs nothing; closing either closes both.

      Derived rather than set on the session, so one caller's sandbox can't leak into another caller's requests. TM1 evaluates NON EMPTY and filters in the request's sandbox, so every request of an operation needs it, cellset reads and deletes included; a service given this view gets that for free.

      Parameters:
      name - the sandbox's name; not blank
      Returns:
      a view of this session in that sandbox
      Throws:
      IllegalArgumentException - if name is blank
    • withoutSandbox

      public Tm1Session withoutSandbox()
      A view of this session whose requests run outside any sandbox, in base data.
      Returns:
      the view, or this session if it has no sandbox
    • batch

      public Tm1SessionBatch batch()
      Starts a JSON $batch: its session is a view of this one (same sandbox and change set) whose requests are queued, and Tm1SessionBatch.run() sends them together.
      Returns:
      the batch
    • withAsync

      public Tm1Session withAsync(Duration every)
      A view of this session whose requests TM1 runs asynchronously (OData's Prefer: respond-async): each returns at once with a monitor, which the client polls every every until the result is there, so a long process or chore never holds a request open for its whole run. Methods return what they always do. Cancelling the subscription before the result deletes the monitor, which stops the work on TM1: a process run ends as TM1UserException: Cancel.

      Raw byte requests (document content) aren't sent async. TM1 (11.8) sends no Retry-After, so every is the only pace.

      Parameters:
      every - how long to wait between polls of the monitor
      Returns:
      the view
      Throws:
      IllegalArgumentException - if every isn't positive
    • withoutAsync

      public Tm1Session withoutAsync()
      A view of this session whose requests run synchronously, as usual.
      Returns:
      the view, or this session if it isn't async
    • async

      public Optional<Duration> async()
      How often this view polls an async request's monitor, if it's async.
      Returns:
      the interval, or empty for a synchronous view
    • sandbox

      public Optional<String> sandbox()
      The sandbox this view's requests run in, if any.
      Returns:
      the sandbox's name, or empty for base data
    • withChangeSet

      public Tm1Session withChangeSet(String id)
      A view of this session whose writes are recorded in change set id: each request gets TM1's !ChangeSet query option, unless it sets its own, so UndoChangeSet can revert them together. Like withSandbox(String), the view shares this session's login and connections; it keeps the sandbox, if any.
      Parameters:
      id - the change set's ID, from BeginChangeSet; not blank
      Returns:
      a view of this session recording into that change set
      Throws:
      IllegalArgumentException - if id is blank
    • withoutChangeSet

      public Tm1Session withoutChangeSet()
      A view of this session whose writes go into no change set.
      Returns:
      the view, or this session if it has no change set
    • changeSet

      public Optional<String> changeSet()
      The change set this view's writes are recorded in, if any.
      Returns:
      the change set's ID, or empty
    • connect

      public reactor.core.publisher.Mono<Tm1Session> connect()
      Logs in now, rather than on the first request, and completes with this session.
      Returns:
      this session, once logged in
    • sessionId

      public reactor.core.publisher.Mono<Long> sessionId()
      The TM1 session's ID, as listed in Sessions. Logs in first if needed.
      Returns:
      the session ID
    • close

      public reactor.core.publisher.Mono<Void> close()
      Closes the TM1 session. A login in progress is waited for and then closed, so a close that races the first request can't leave a session open. If no login ever started, nothing is sent. Afterwards every request fails with Tm1SessionClosedException. Calling it again returns the same result.
      Returns:
      completes once the session is closed
    • isClosed

      public boolean isClosed()
      Whether close() has been called.
      Returns:
      true once closed
    • serviceRoot

      public String serviceRoot()
      The service root, such as http://localhost:8010/api/v1/.
      Returns:
      the service root, always ending in /
    • toString

      public String toString()
      Overrides:
      toString in class Object