2016-09-02 6 views
10

मैं this के माध्यम से रहा हूं और समझ गया कि मुझे कनेक्शन स्वीकार करने के लिए TcpReceivingChannelAdapter बनाने की आवश्यकता है। लेकिन मुझे नहीं पता कि इसके साथ कैसे आगे बढ़ना है।कनेक्शन स्वीकार करने के लिए वसंत बूट में एक टीसीपी कनेक्शन कैसे बनाएं?

क्या कोई मुझे इस पर मार्गदर्शन कर सकता है?

उत्तर

11

XML कॉन्फ़िगरेशन का उपयोग कर कुछ पॉइंटर्स के लिए tcp-client-server sample देखें।

जावा कॉन्फ़िगरेशन के लिए; यहां एक साधारण स्प्रिंग बूट ऐप है ...

package com.example; 

import java.net.Socket; 

import javax.net.SocketFactory; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.ConfigurableApplicationContext; 
import org.springframework.context.annotation.Bean; 
import org.springframework.integration.annotation.ServiceActivator; 
import org.springframework.integration.annotation.Transformer; 
import org.springframework.integration.channel.DirectChannel; 
import org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter; 
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory; 
import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory; 
import org.springframework.integration.transformer.ObjectToStringTransformer; 
import org.springframework.messaging.MessageChannel; 

@SpringBootApplication 
public class So39290834Application { 

    public static void main(String[] args) throws Exception { 
     ConfigurableApplicationContext context = SpringApplication.run(So39290834Application.class, args); 
     Socket socket = SocketFactory.getDefault().createSocket("localhost", 9999); 
     socket.getOutputStream().write("foo\r\n".getBytes()); 
     socket.close(); 
     Thread.sleep(1000); 
     context.close(); 
    } 

    @Bean 
    public TcpNetServerConnectionFactory cf() { 
     return new TcpNetServerConnectionFactory(9999); 
    } 

    @Bean 
    public TcpReceivingChannelAdapter inbound(AbstractServerConnectionFactory cf) { 
     TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter(); 
     adapter.setConnectionFactory(cf); 
     adapter.setOutputChannel(tcpIn()); 
     return adapter; 
    } 

    @Bean 
    public MessageChannel tcpIn() { 
     return new DirectChannel(); 
    } 

    @Transformer(inputChannel = "tcpIn", outputChannel = "serviceChannel") 
    @Bean 
    public ObjectToStringTransformer transformer() { 
     return new ObjectToStringTransformer(); 
    } 

    @ServiceActivator(inputChannel = "serviceChannel") 
    public void service(String in) { 
     System.out.println(in); 
    } 

} 
+1

बिल्कुल सही! क्या आप मुझे बता सकते हैं कि स्प्रिंग बूट में जावा आधारित कॉन्फ़िगरेशन पर मुझे दस्तावेज़ कहां मिल सकता है? – amitection

+2

मुझे यकीन नहीं है कि आपका क्या मतलब है। यदि आपका मतलब बूट में वसंत एकीकरण घटकों की जावा कॉन्फ़िगरेशन है, तो हमारे पास संदर्भ मैनुअल में कुछ उदाहरण हैं, लेकिन अभी तक सभी मॉड्यूल के लिए नहीं। आपके पास मार्गदर्शन करने के लिए हमारे पास [अनुभाग] (http://docs.spring.io/spring-integration/reference/html/overview.html#programming-tips) है और आपको इंगित करता है कि सबसे अधिक के लिए अंतर्निहित घटकों के बारे में जानकारी कहां प्राप्त करें तत्वों। –

+0

यह वही है जो मेरा मतलब था। अनेक अनेक धन्यवाद! – amitection

संबंधित मुद्दे