【发布时间】:2019-03-29 08:30:39
【问题描述】:
要在使用 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);
}
});
}
};
这会返回“Been there or not? - null”,所以会话监听器不起作用。
Rest config 创建了一个 Jetty 组件路由并添加了 handlers 选项。深入了解调试器,我的印象是我的处理程序被添加到 Jetty 端点调用的时间太晚了,当时会话已经开始,所以它没有任何效果。
如何将我自己的HttpSessionListener 添加到 Camel 中的嵌入式 Jetty 服务器?尽管该组件被称为“jetty”,但 API 似乎无法让我访问 Jetty 的 Server 和其他对象,而且看起来不那么抽象 Jetty 的内部结构看起来很正常。
主要目标是在会话销毁事件中运行一些东西。
更新 - 试图破解它并在处理器中添加会话监听器 - IllegalStateException
【问题讨论】:
标签: apache-camel embedded-jetty