如何将 HttpSessionListener 添加到 Camel 的嵌入式 Jetty
How do I add an HttpSessionListener to Camel's embedded Jetty
要在使用 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);
}
});
}
};
这 returns "Been there or not? - null",所以会话侦听器没有工作。
Rest 配置创建 Jetty 组件路由并添加 handlers
选项。深入研究调试器,我的印象是我的处理程序添加到 Jetty 端点调用的方式太晚了,当时会话已经启动,所以它没有任何效果。
如何将我自己的 HttpSessionListener
添加到 Camel 中的嵌入式 Jetty 服务器? API 似乎无法让我访问 Jetty 的 Server
和其他对象,尽管该组件被称为 "jetty" 并且 Jetty 的内部结构不那么抽象看起来很正常。
主要目标是运行会话中的某些东西销毁事件。
更新 - 试图破解它并在处理器中添加一个会话监听器 - IllegalStateException
你能为你的 camel 实例添加一个标准的 Servlet 或 Filter 吗?
如果是这样,使 init()
将 HttpSessionListener
添加到 ServletContext
并且上述 servlet/filter 的实现成为空操作。
在 init()
期间添加侦听器很重要,因为这是唯一允许完成的时间(在 WebApp 的 startup/init 期间)。
未来的 Camel Jetty 用户。
如果您想使用自己的 HttpSessionListener
,或者更广泛地说,Jetty 的 SessionHandler
,永远不要设置 sessionSupport=true
。它将 SessionHandler
替换为空的 nothing.
然后像往常一样将您的处理程序添加到端点 uri:?handlers=yourSessionHandlerBeanRef
.
在上面的例子中,只需注释掉这一行:
//options.put("sessionSupport", true);
希望我已经为您节省了一两天时间。
要在使用 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);
}
});
}
};
这 returns "Been there or not? - null",所以会话侦听器没有工作。
Rest 配置创建 Jetty 组件路由并添加 handlers
选项。深入研究调试器,我的印象是我的处理程序添加到 Jetty 端点调用的方式太晚了,当时会话已经启动,所以它没有任何效果。
如何将我自己的 HttpSessionListener
添加到 Camel 中的嵌入式 Jetty 服务器? API 似乎无法让我访问 Jetty 的 Server
和其他对象,尽管该组件被称为 "jetty" 并且 Jetty 的内部结构不那么抽象看起来很正常。
主要目标是运行会话中的某些东西销毁事件。
更新 - 试图破解它并在处理器中添加一个会话监听器 - IllegalStateException
你能为你的 camel 实例添加一个标准的 Servlet 或 Filter 吗?
如果是这样,使 init()
将 HttpSessionListener
添加到 ServletContext
并且上述 servlet/filter 的实现成为空操作。
在 init()
期间添加侦听器很重要,因为这是唯一允许完成的时间(在 WebApp 的 startup/init 期间)。
未来的 Camel Jetty 用户。
如果您想使用自己的 HttpSessionListener
,或者更广泛地说,Jetty 的 SessionHandler
,永远不要设置 sessionSupport=true
。它将 SessionHandler
替换为空的 nothing.
然后像往常一样将您的处理程序添加到端点 uri:?handlers=yourSessionHandlerBeanRef
.
在上面的例子中,只需注释掉这一行:
//options.put("sessionSupport", true);
希望我已经为您节省了一两天时间。