【问题标题】:How do I add an HttpSessionListener to Camel's embedded Jetty如何将 HttpSessionListener 添加到 Camel 的嵌入式 Jetty
【发布时间】: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


    【解决方案1】:

    您可以向骆驼实例添加标准 Servlet 或过滤器吗?

    如果是这样,则使init()HttpSessionListener 添加到ServletContext 并且所述servlet/过滤器的实现是无操作的。

    init() 期间添加监听器很重要,因为这是唯一允许完成的时间(在 WebApp 的启动/初始化期间)。

    【讨论】:

    • 我不确定我能不能,必须调查它或询问如何去做
    • 不,不幸的是我还没有找到如何在服务启动之前将 servlet 添加到码头 servlet 上下文中,否则当我添加任何内容作为上下文侦听器时,状态再次是非法的跨度>
    【解决方案2】:

    未来的骆驼码头用户。

    如果您想使用自己的HttpSessionListener,或者更广泛地说,Jetty 的SessionHandler,请永远不要设置sessionSupport=true。它会将您的SessionHandler 替换为一个什么都不做的空邮件。

    然后像往常一样将您的处理程序添加到端点 uri:?handlers=yourSessionHandlerBeanRef

    在上面的例子中,只需注释掉这一行:

    //options.put("sessionSupport", true);
    

    希望我救了你一两天。

    【讨论】:

    • 编辑吃光了我的问候。帖子以“Hello,”子字符串开头。但我很生气,我在这个问题上浪费了两天时间。我需要表达我的被动攻击性
    猜你喜欢
    • 2013-11-01
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多