【问题标题】:Sockets and Spring Integration套接字和 Spring 集成
【发布时间】:2012-03-03 10:29:13
【问题描述】:

我有一个小程序,监听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-context 用于实现相同目的的东西:

<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 端口上提供数据。

希望你能帮忙。

谢谢

【问题讨论】:

  • 在 spring 开始新的实现之前,你是否停止了监听 30003 端口的程序?
  • 是的。有一个在 30003 上运行的 应用程序 提供我正在尝试收听的数据。套接字方法在该应用程序运行时运行良好,但此 SI 产生了错误。
  • 请注意,监听端口意味着打开一个服务器套接字,并且不能有两个或多个应用程序打开同一个端口。您的 spring 配置尝试在端口 30003 上打开 ServerSocket,因此您收到异常,地址已在使用中。

标签: java spring sockets port spring-integration


【解决方案1】:

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

要“监听”端口“30003”,我必须生成一个“客户端”类型的 connectionFactory:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-22
    • 2017-10-02
    • 2015-01-23
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2015-01-29
    • 2013-03-08
    相关资源
    最近更新 更多