If you are sending a message to an ErraiBus service method, you will have the Message
object available. You can retrieve the session from it and get that session's ID like this:
@Service
public class ClientHelloService implements MessageCallback {
@Override
public void callback(final Message message) {
HttpSession session = message.getResource(
HttpServletRequest.class, HttpServletRequest.class.getName()).getSession();
System.out.println("Client said hello. Session ID: " + session.getId());
}
}
If you are instead sending the message to an Errai RPC endpoint, you will not have such easy access to the message. In this case, you will have to use the RpcContext.getSession()
method:
@Service
public class ClientHelloRpcServiceImpl implements ClientHelloRpcService {
@Override
public void hello() {
HttpSession session = RpcContext.getHttpSession();
System.out.println("Client said hello. Session ID: " + session.getId());
}
}
The way this works is simple but ugly: RpcContext class stores the Message object that contained the RPC request in a ThreadLocal, and it just retrieves the HttpSession from that.