Reputation: 941
To set an HttpSessionListener to Camel's embedded Jetty while using Rest I have tries this:
SessionHandler sess = new SessionHandler();
sess.addEventListener(new HttpSessionListener() {
@Override
public void sessionCreated(HttpSessionEvent se) {
// some code
se.getSession().setAttribute("WasHere", true);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
// some cleanup code that really can't be palced anywhere else
}
});
String sessionHandlerString = "jettySessionHandler";
_integration.getRegistry().put(sessionHandlerString, sess); // this works
String port = _properties.getProperty("port");
RestConfiguration restConfiguration = new RestConfiguration();
restConfiguration.setComponent("jetty");
HashMap<String, Object> options = new HashMap<>();
options.put("sessionSupport", true);
options.put("handlers", sessionHandlerString);
restConfiguration.setEndpointProperties(options);
restConfiguration.setHost("localhost");
restConfiguration.setPort(Integer.parseInt(port));
restConfiguration.setBindingMode(RestConfiguration.RestBindingMode.auto);
_integration.getContext().setRestConfiguration(restConfiguration);
// getting an object
JettyHttpComponent9 jettyComponent = _integration.getContext().getComponent("jetty", JettyHttpComponent9.class);
RouteBuilder rb = new RouteBuilder(_integration.getContext()) {
@Override
public void configure() throws Exception {
rest("/test/path")
.get().route().process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
HttpMessage msg = exchange.getIn(HttpMessage.class);
Object ret = msg.getRequest().getSession().getAttribute("WasHere");
msg.setBody("Been there or not? - " + ret);
}
});
}
};
This returns "Been there or not? - null", so the session listener didn't work.
Rest config makes a Jetty component route and does add the handlers
option. Diving down with the debugger I had an impression that my handler was added to the Jetty endpoint call way too late, when the session had been already started, so it didn't have any effect.
How do I add my own HttpSessionListener
to the embedded Jetty server in Camel? The API doesn't seem to provide me access to the Jetty's Server
and other objects despite the component being called "jetty" and it looks normal not to be so abstract of the Jetty's internals.
The main goal is to run something in the session destroy event.
update - tried to hack it and add a session listener in a processor - IllegalStateException
Upvotes: 0
Views: 293
Reputation: 941
future Camel Jetty user.
If you want to use your own HttpSessionListener
or, in broader sense, Jetty's SessionHandler
, just never set sessionSupport=true
. It replaces your SessionHandler
with an empty one that does nothing.
Then add your handler to the endpoint uri as you would normally do: ?handlers=yourSessionHandlerBeanRef
.
In the example above, just comment out this line:
//options.put("sessionSupport", true);
Hope I've saved you a day or two.
Upvotes: 0
Reputation: 49515
Can you add a standard Servlet or Filter to your camel instance?
If so, make the init()
add the HttpSessionListener
to the ServletContext
and the implementation of said servlet/filter be a no-op.
It's important to add the listener this during the init()
as that's the only time its allowed to be done (during the startup/init of a WebApp).
Upvotes: 0