本文介绍了结合Netty和Spring MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Spring MVC中配置Netty。何时何地应该启动Netty tcp服务器?一旦Spring开始,我应该初始化netty吗?有人可以给我看一个例子,比如Spring配置xml文件或eles吗?
谢谢!

How can I configure Netty in Spring MVC. When and where should I start the Netty tcp server? Should I init netty once the Spring starts? Could someone show me an example such as the Spring configuration xml file or something eles?Thanks!

推荐答案

这真的取决于你使用Netty的原因。假设您将其用作在单独端口上运行的嵌入式HTTP服务器,您可以简单地在Spring bean中初始化它。我过去使用一个名为的有用的Netty / Atmosphere包装器实现了这一点:

It really depends what you are using Netty for. Assuming you are using it as an embedded HTTP server running on a separate port, you could simply initialise it within a Spring bean. I've achieved this in the past using a useful Netty/Atmosphere wrapper called Nettosphere:

@Service
public class NettyServer implements ServletContextAware {

    private ServletContext servletContext;

    private Nettosphere server;

    @Autowired
    private MyStatusHandler statusHandler;

    @PostConstruct
    public void initialiseNettyServer() {
            String rootPath = servletContext.getContextPath() + "/api";

            server = new Nettosphere.Builder().config(
                    new Config.Builder()
                            .host(SERVER_HOST)
                            .port(SERVER_PORT)
                            .resource(rootPath + "/status", statusHandler)
                            .build())
                     .build();
            server.start();
    }

    @PreDestroy
    public void shutdownNettyServer() {
            server.stop();
    }

}

这假定在Spring中,您可以使用XML轻松实现相同的结果,如中所述。

This assumes the annotation-based configuration in Spring, you can easily achieve the same result using XML as explained in Jonathan's answer.

当然,您可能更喜欢直接使用Netty,在这种情况下适用相同的原则,但您需要深入研究,以便正确引导服务器。

Of course, you may prefer to use Netty directly, in which case the same principle applies, but you will need to dig into the Netty user guide a bit to bootstrap the server correctly.

这篇关于结合Netty和Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 15:58