java - 套接字和 Spring 集成

标签 java spring sockets port spring-integration

我有一个小程序,监听端口30003。它是一个服务器类,处理字节流数据如下:

sbsSocket = new Socket(sbsHost, sbsPort);
sbsReader = new BufferedReader(new InputStreamReader(sbsSocket.getInputStream()));

private void startSBSMessageReceiverThread() {
    new Thread(new Runnable() {
        public void run() {
            while(true) {
                try {
                    String message = sbsReader.readLine();
                    // System.out.println(message);
                    tracker.handleSBSMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

这工作得很好 - 但是我想转向使用 Spring Integration。这是我的 tcp 上下文,旨在实现相同的目标:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:ip="http://www.springframework.org/schema/integration/ip"
    xsi:schemaLocation="http://www.springframework.org/schema/integration 
http://www.springframework.org/schema/integration/spring-integration-2.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/integration/ip
http://www.springframework.org/schema/integration/ip/spring-integration-ip-2.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">


<context:component-scan base-package="com.atlaschase.falcon.integration"></context:component-scan>

<int:channel id="heathrowChannel"></int:channel>

<ip:tcp-connection-factory id="heathrowConnectionFactory" type="server" host="127.0.0.1" port="30003"/>

<ip:tcp-inbound-channel-adapter id="heathrowInboundAdapter" channel="heathrowChannel" connection-factory="heathrowConnectionFactory"/>

<int:service-activator id="adsbHandler" input-channel="heathrowChannel" ref="sbsListener"/>

</beans>

当我使用 Spring Integration 方法启动程序时,我得到以下堆栈跟踪 - 告诉我套接字地址已在使用中。

SEVERE: Error on ServerSocket
java.net.BindException: Address already in use: JVM_Bind
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)
    at java.net.ServerSocket.bind(ServerSocket.java:328)
    at java.net.ServerSocket.<init>(ServerSocket.java:194)
    at java.net.ServerSocket.<init>(ServerSocket.java:150)
    at javax.net.DefaultServerSocketFactory.createServerSocket(ServerSocketFactory.java:163)
    at org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory.run(TcpNetServerConnectionFactory.java:61)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

我不明白为什么当我的标准套接字程序工作正常时 - 为什么集成方法失败了。我的机器上有一些软件可以在 30003 端口上提供数据。

希望能帮到你。

谢谢

最佳答案

这是通过消除服务器和客户端之间的混淆来解决的。我无法生成服务器套接字,因为应用程序已绑定(bind)到该端口号。

为了“监听”端口“30003”,我必须生成一个“client”类型的connectionFactory:

<ip:tcp-connection-factory id="heathrowConnectionFactory" type="server" host="127.0.0.1" port="30003"/>

关于java - 套接字和 Spring 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9236499/

相关文章:

c - 选择在 Linux 上超时之前返回的调用

java - jsp页面中的ChoiceBox迭代

java - 重新初始化 spring @Autowire bean

html - 浏览器将C服务器发送过来的图像文件显示为一长串字符

java - 生成 keystore 时遇到问题 (Java)

spring - ContextLoader - 根 WebApplicationContext 在 ubuntu Tomcat 上初始化 3 次

java: bind exception address in use 未使用时报错(netstat显示)

java - 如何消除我的应用程序中的希腊字符?

java - 为什么 Jtable 中行高自动调整不起作用

java - 迭代多线程列表而不同步整个进程