简体   繁体   English

如何将Hessian与Guice集成?

[英]How to integrate Hessian with Guice?

We're looking at technologies for an up and coming project here and I really want to use Guice as our dependency injection framework, I also want to use Hessian for client/server comms, but it doesn't seem to be compatible with Guice. 我们正在这里寻找一个即将到来的项目的技术,我真的想使用Guice作为我们的依赖注入框架,我也想使用Hessian进行客户端/服务器通信,但是它似乎与Guice不兼容。

public class WebMobule extends ServletModule {

@Override
protected void configureServlets() {

    serve("/fileupload").with(FileUploadServlet.class);

    // this doesn't work! AuthenticationServlet extends HessianServlet
    // HessianServlet extends GenericServlet - Guice wants something that extends
    // HttpServlet
    serve("/authentication").with(AuthenticationServlet.class); 

}

Has anyone managed to solve this problem - if so how did you do it? 有没有人设法解决这个问题-如果是这样,您是如何做到的?

cheers 干杯

Phil 菲尔

I would write a custom HessianHttpServlet which extends HttpServlet and delegates method calls to a encapsulated HessianServlet. 我将编写一个自定义的HessianHttpServlet,它扩展HttpServlet并将方法调用委托给封装的HessianServlet。 In this way the Guice serve call will be satiated and you will be using HessianServlet behavior. 这样,Guice服务调用将得到满足,并且您将使用HessianServlet行为。

I created a little open source project which enables easy integration of hessian and guice. 我创建了一个开源项目,可以轻松地将粗麻布和guice集成在一起。 You can use annotation based configuration like this: WebService: 您可以使用基于注释的配置,如下所示:WebService:

@HessianWebService
public class UserServiceImpl implements UserService {
    ...
}

Guice configuration: Guice配置:

public class WebServiceGuiceServletContextListener extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(
                /* your guice modules */
                new HessianWebServicesModule("your web service implementations package")
        );
    }
}

or the manual way using the EDSL: 或使用EDSL的手动方式:

public class WebServiceGuiceServletContextListener extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(
                /* your guice modules */
                new HessianWebServicesModule(){
                    @Override
                    protected void configureHessianWebServices() {
                        serveHessianWebService(UserService.class).usingUrl("/Users");
                    }
                }
        );
    }
}

More information, configuration options and complete examples are available here: https://bitbucket.org/richard_hauswald/hessian-guice/ 有关更多信息,配置选项和完整示例,请参见: https//bitbucket.org/richard_hauswald/hessian-guice/

It needs some work, but fundamentally I solved the problem with this (thanks Syntax!): 它需要一些工作,但是从根本上来说,我解决了这个问题(感谢语法!):

@Singleton
public class AuthenticationWrapperServlet extends HttpServlet {

    private static final Logger LOG = Logger.getLogger(HessianDelegateServlet.class);

    // this is the HessianServlet
    private AuthenticationServlet authenticationServlet;

    @Override
    public void init(ServletConfig config) throws ServletException {
        LOG.trace("init() in");
        try {
            if (authenticationServlet == null) {
                authenticationServlet = new AuthenticationServlet();
            }
            authenticationServlet.init(config);
        } catch (Throwable t) {
            LOG.error("Error initialising hessian servlet", t);
            throw new ServletException(t);
        }
        LOG.trace("init() out");
    }

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {

        try {
            authenticationServlet.service(request, response);
        } catch (Throwable t) {
            LOG.error("Error calling service()", t);
            throw new ServletException(t);
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM