2016-09-02 19 views

cevap

11

XML yapılandırmasını kullanan bazı işaretçiler için tcp-client-server sample'a bakın.

Java yapılandırması için; İşte basit bir Spring Boot uygulaması ...

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

Mükemmel! Spring boot'ta Java tabanlı konfigürasyon ile ilgili belgeleri nerede bulabileceğimi gösterebilir misiniz? – amitection

+2

Ne demek istediğinden emin değilim. Önyükleme sırasında Spring Integration bileşenlerinin java konfigürasyonunu kastediyorsanız, referans kılavuzunda bazı örneklere sahibiz, ancak henüz tüm modüller için değil. Size rehberlik etmek için [bölüm] (http://docs.spring.io/spring-integration/reference/html/overview.html#programming-tips) adresimiz var. elementler. –

+0

Bu tam olarak benim kastettiğim şey. Teşekkürler bir ton! – amitection

İlgili konular