Session Context
Overview
Apache Ignite allows setting custom application attributes at runtime. These attributes may include session ID, application language, application name, etc. The attributes are set by the application only once for the Ignite API entry point (Ignite, JDBC) and are available on all participating nodes for application requests and queries.
SessionContext Interface
SessionContext is an entry point for accessing attributes at all levels and other session-level data. Ignite provides SessionContextProviderResource annotation for access to SessionContext. User-defined functions, such as QuerySqlFunction and CacheInterceptor, can access these attributes using SessionContextProvider.
/** SessionContext interface. It's an entrypoint for all attributes. */
public interface SessionContext {
/** @return Attribute by name. */
public @Nullable String getAttribute(String attrName);
}
Application Attributes in QuerySqlFunction
User-defined SQL functions, like QuerySqlFunction, can utilize application attributes set on the client side:
public class MyFunctions {
@SessionContextProviderResource
public SessionContextProvider sesCtxProv;
@QuerySqlFunction
public String sessionId() {
return sesCtxProv.getSessionContext().getAttribute("SESSION_ID");
}
}
|
Note
|
Attribute access is only available in the Calcite query engine. In such cases, the class must have a public zero-argument constructor. |
Application Attributes in CacheInterceptor
You can configure a custom CacheInterceptor and write callbacks with access to application attributes, which are invoked before and after main cache operations:
public class SessionContextCacheInterceptor implements CacheInterceptor<Integer, String> {
/** */
@SessionContextProviderResource
private SessionContextProvider sesCtxPrv;
/** */
@Override public @Nullable String onGet(Integer key, @Nullable String val) {
String ret = sesCtxPrv.getSessionContext().getAttribute("onGet");
return ret == null ? val : ret + key;
}
/** */
@Override public @Nullable String onBeforePut(Cache.Entry<Integer, String> entry, String newVal) {
String ret = sesCtxPrv.getSessionContext().getAttribute("onBeforePut");
return ret == null ? newVal : ret + entry.getKey();
}
/** */
@Override public @Nullable IgniteBiTuple<Boolean, String> onBeforeRemove(Cache.Entry<Integer, String> entry) {
String ret = sesCtxPrv.getSessionContext().getAttribute("onBeforeRemove");
return new IgniteBiTuple<>(ret != null, entry.getValue());
}
}
Setting application attributes
Ignite
You can set attributes using Ignite.withApplicationAttributes(…). It returns an Ignite instance aware of the attributes. Application attributes are propagated to remote nodes:
try (Ignite ign = Ignition.start(ignCfg)) {
Map<String, String> appAttrs = F.asMap("SESSION_ID", "1234");
Ignite app = Ignite.withApplicationAttributes(appAttrs);
//Your code here...
}
JDBC
The standard JDBC protocol provides the method Connection.setClientInfo(…) to set client attributes, which are available during the connection’s lifetime. Attributes can be overriden by invoking Connection.setClientInfo(…) again with new parameters:
// JDBC connection to Ignite server.
try (Connection conn = DriverManager.getConnection(URL)) {
conn.setClientInfo("SESSION_ID", "1234");
//Your code here...
}
Apache, Apache Ignite, the Apache feather and the Apache Ignite logo are either registered trademarks or trademarks of The Apache Software Foundation.