开发者

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 clien开发者_运维知识库t/server comms, but it doesn't seem to be compatible with 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. In this way the Guice serve call will be satiated and you will be using HessianServlet behavior.


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);
        }

    }
}


I created a little open source project which enables easy integration of hessian and guice. You can use annotation based configuration like this: WebService:

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

Guice configuration:

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:

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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜