You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求Spring Integration 4.3.14 TCP服务器Java DSL响应消息示例

Got it, let's put together a simple, runnable example that fits your exact requirements: Spring Integration 4.3.14, Java DSL only, TCP server that receives string payloads from a PLC and sends back a formatted response.

Spring Integration 4.3.14 TCP Server Java DSL Example (No XML)

Step 1: Add Compatible Dependencies

First, ensure your Maven pom.xml has these dependencies (Spring Boot 1.5.x aligns perfectly with Spring Integration 4.3.x):

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-integration</artifactId>
        <version>1.5.19.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-ip</artifactId>
        <version>4.3.14.RELEASE</version>
    </dependency>
</dependencies>

Step 2: Core TCP Server Configuration

This config sets up the TCP server connection factory and a request-response gateway—this gateway is the key to sending responses back (you were likely using a one-way adapter before, which can't reply):

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.support.Transformers;
import org.springframework.integration.ip.dsl.Tcp;
import org.springframework.integration.ip.tcp.TcpNetServerConnectionFactory;
import org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer;

@Configuration
public class TcpServerConfig {

    // Configure TCP server to listen on port 9876
    @Bean
    public TcpNetServerConnectionFactory tcpServerConnectionFactory() {
        TcpNetServerConnectionFactory factory = new TcpNetServerConnectionFactory(9876);
        // Use CrLf serializer if your PLC uses \r\n to mark end of messages
        // Swap to ByteArrayRawSerializer if there's no delimiter (you'll need to handle message boundaries manually)
        factory.setSerializer(new ByteArrayCrLfSerializer());
        factory.setDeserializer(new ByteArrayCrLfSerializer());
        return factory;
    }

    // Set up the inbound gateway for request-response communication
    @Bean
    public IntegrationFlow tcpInboundGatewayFlow() {
        return IntegrationFlows.from(Tcp.inboundGateway(tcpServerConnectionFactory()))
                .transform(Transformers.objectToString()) // Convert raw byte payload to String
                .handle("plcMessageHandler", "processMessage") // Pass to our custom handler
                .get();
    }
}

Step 3: Custom Payload Handler Service

This is where you'll implement your logic to process the incoming PLC string and generate the formatted response:

import org.springframework.stereotype.Service;

@Service("plcMessageHandler")
public class PlcMessageHandler {

    public String processMessage(String inputPayload) {
        // Log incoming payload for debugging
        System.out.println("Received from PLC: " + inputPayload);

        // Replace this with your actual response logic
        // Example: Echo back the input with a PLC-compatible response prefix
        return "PLC_ACK: " + inputPayload;
    }
}

Step 4: Run the Server

Use a simple Spring Boot app to start the TCP server:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TcpServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(TcpServerApplication.class, args);
        System.out.println("TCP Server running on port 9876... Ready for PLC connections.");
    }
}

Critical Notes for Your Legacy PLC

  • Request-Response Gateway: The TcpInboundGateway is what enables sending responses—unlike one-way adapters, it waits for a handler to return a message to send back to the PLC.
  • Serializers: The ByteArrayCrLfSerializer works if your PLC uses \r\n to delimit messages. If your PLC uses a different delimiter or fixed-length messages, adjust the serializer (e.g., ByteArrayRawSerializer plus a custom message boundary detector).
  • String Processing: The objectToString transformer converts the raw TCP byte stream directly to a String, so you can handle all parsing/formatting logic in the PlcMessageHandler—perfect for your legacy system with variable payload formats.

Test the Server Quickly

You can test this with telnet or nc (netcat):

  1. Open a terminal and run telnet localhost 9876
  2. Type a test string (e.g., PLC_REQUEST: MACHINE_STATUS) and press enter
  3. You'll get a response like PLC_ACK: PLC_REQUEST: MACHINE_STATUS

内容的提问来源于stack exchange,提问作者Ian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:32:19