我正在使用Spring boot.application,该应用程序创建使用Netty实现的TCP服务器。我已经创建了应用程序并对其进行了测试,并且一切正常。
问题是当我想从源代码构建jar时,没有创建jar.TCP服务器启动并响应客户端请求,但未在目标文件夹中创建jar。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages={"some.package"})
public class OtpServiceApplication implements ApplicationRunner
{
@Autowired
TcpServer tcpServer;
@Override
public void run(ApplicationArguments args) throws Exception
{
tcpServer.start();
}
public static void main(String[] args)
{
SpringApplication.run(OtpServiceApplication.class, args);
}
}
但是,当我删除与TCP相关的代码时,jar创建成功。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages={"some.package"})
public class OtpServiceApplication
{
// @Autowired
// TcpServer tcpServer;
//
// @Override
// public void run(ApplicationArguments args) throws Exception
// {
// tcpServer.start();
// }
public static void main(String[] args)
{
SpringApplication.run(OtpServiceApplication.class, args);
}
}
因此,我想我的POM文件或代码不是问题,但在此类中是一个问题。有人可以指出如何解决这个问题吗?
这是我的TcpServer类。
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
public class TcpServer {
@Autowired
XmlProcessor xmlProcessor;
@Autowired
IdleSocketHandler idleSocketHandler;
@Value("${request.timeout:5000}")
String requestTimeout;
@Value("${response.timeout:2000}")
String responseTimeout;
@Value("${message-listener.port}")
String xmlListenerPort;
@Value("${message-listener.ip}")
String xmlListenerIp;
private NioEventLoopGroup connectionAcceptorThreadGroup;
private NioEventLoopGroup connectionProcessorThreadGroup;
private ChannelFuture serverSocketFuture;
@Async
public void start() throws InterruptedException {
log.info("Starting XML Listener on [{}:{}]", xmlListenerIp, xmlListenerPort);
this.connectionAcceptorThreadGroup = new NioEventLoopGroup();
this.connectionProcessorThreadGroup = new NioEventLoopGroup();
final int resTimeout = Integer.valueOf(responseTimeout);
final int reqTimeout = Math.min(Integer.valueOf(requestTimeout), (int) (resTimeout * 1.8));
try {
ServerBootstrap b = new ServerBootstrap();
b.group(connectionAcceptorThreadGroup, connectionProcessorThreadGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ch.pipeline().addLast("requestHandler", xmlProcessor);
ch.pipeline().addLast("idleStateHandler",
new IdleStateHandler(reqTimeout, resTimeout, 0, TimeUnit.MILLISECONDS));
ch.pipeline().addLast("idleSocketHandler", idleSocketHandler);
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.option(ChannelOption.SO_REUSEADDR,true)
.childOption(ChannelOption.SO_KEEPALIVE, true);
serverSocketFuture = b.bind(xmlListenerIp, Integer.valueOf(xmlListenerPort)).sync();
log.info("Server listening for transactions on {}:{}", xmlListenerIp, xmlListenerPort);
serverSocketFuture.channel().closeFuture().sync();
} finally {
connectionProcessorThreadGroup.shutdownGracefully();
connectionAcceptorThreadGroup.shutdownGracefully();
}
}
}
答案 0 :(得分:0)
您不能在下面的类中自动布线,因为在自动装配任何类之前,它应该是遵循原型概念的那些类的一部分,因此请根据您的逻辑用@ Component,@ Servive ... etc创建另一个类,然后在该类中自动装配
@Autowired
TcpServer tcpServer;
您可以尝试将此类强加于除主类之外的其他位置,并尝试创建一个jar。
答案 1 :(得分:0)
不是自动装配,而是获取应用程序上下文,以便像这样创建Bean
ConfigurableApplicationContext applicationContext =
SpringApplication.run(OtpServiceApplication.class, args);
TcpSrever tcpServer = applicationContext.getBean(TcpServer.class);
tcpServer.run()