要在使用Rest时将HttpSessionListener设置为Camel的嵌入式Jetty,我试过这个:
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);
}
});
}
};
这将返回“是否存在? - null”,因此会话侦听器不起作用。
Rest配置生成Jetty组件路由并添加handlers
选项。使用调试器深入了解我的印象是,当会话已经启动时,我的处理程序被添加到Jetty端点调用方式太晚了,所以它没有任何效果。
如何将自己的HttpSessionListener
添加到Camel中的嵌入式Jetty服务器? API似乎并没有让我访问Jetty的Server
和其他对象,尽管该组件被称为“jetty”,并且看起来不是如此抽象的Jetty的内部结构。
主要目标是在会话销毁事件中运行一些东西。
更新 - 尝试破解它并在处理器中添加会话侦听器 - IllegalStateException
你可以为你的camel实例添加一个标准的Servlet或Filter吗?
如果是这样,让init()
将HttpSessionListener
添加到ServletContext
,并且所述servlet / filter的实现是无操作。
在init()
期间添加监听器非常重要,因为这是唯一允许完成的时间(在WebApp的启动/初始化期间)。
未来的Camel Jetty用户。
如果你想使用你自己的HttpSessionListener
,或者在更广泛的意义上,Jetty的SessionHandler
,只是从来没有设置sessionSupport=true
。它取代你的SessionHandler
,一个空的,什么都不做。
然后像往常一样将处理程序添加到端点uri:?handlers=yourSessionHandlerBeanRef
。
在上面的示例中,只需注释掉这一行:
//options.put("sessionSupport", true);
希望我已经救了你一两天。