求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.
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
TcpInboundGatewayis 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
ByteArrayCrLfSerializerworks if your PLC uses\r\nto delimit messages. If your PLC uses a different delimiter or fixed-length messages, adjust the serializer (e.g.,ByteArrayRawSerializerplus a custom message boundary detector). - String Processing: The
objectToStringtransformer converts the raw TCP byte stream directly to a String, so you can handle all parsing/formatting logic in thePlcMessageHandler—perfect for your legacy system with variable payload formats.
Test the Server Quickly
You can test this with telnet or nc (netcat):
- Open a terminal and run
telnet localhost 9876 - Type a test string (e.g.,
PLC_REQUEST: MACHINE_STATUS) and press enter - You'll get a response like
PLC_ACK: PLC_REQUEST: MACHINE_STATUS
内容的提问来源于stack exchange,提问作者Ian

