10 months ago

首先我們介紹UDP,咱們UDP大神就是秉持著射後不理的態度進行傳輸,完完全全的快速,但射後不理有可能造成資料遺失,又但是呢~用這種UDP的應用,就是時效性大於可靠性時的應用才會大力使用。

Dependencies

//Spring

def springVersion = '5.0.1.RELEASE'
compile group: 'org.springframework', name: 'spring-context', version: "${springVersion}"
compile group: 'org.springframework', name: 'spring-test', version: "${springVersion}"

def springIntVersion = '5.0.0.RELEASE'
compile group: 'org.springframework.integration', name: 'spring-integration-core', version: "${springIntVersion}"
compile group: 'org.springframework.integration', name: 'spring-integration-ip', version: "${springIntVersion}"

//Log4j 2

def log4jVersion = '2.8.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: "${log4jVersion}"
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: "${log4jVersion}"
compile group: 'org.apache.logging.log4j', name: 'log4j-jcl', version: "${log4jVersion}"

//common

compile group: 'commons-collections', name: 'commons-collections', version: '3.2'

//test

testCompile group: 'junit', name: 'junit', version: '4.+'

Integration Config

首先,大家可以看到我們有一個ChannelAdapter,它是作為系統與系統連接的端點,我們將這個端點的輸出通道設定一個OutputChannel,到這邊以後,我們可以簡單利用DLS進行IntegrationFlow的設定,整個UDP Integrationg 設定如下:

@Configuration
@EnableIntegration
public class UdpIntegrationConfig {
    
  // 處理接收到資料的邏輯

    @Bean
    public UdpMessageHandler udpMessageHandler() {
        return new UdpMessageHandler();
    }
    
  // 輸出通道

    @Bean
    public MessageChannel outputChannel() {
        return new DirectChannel();
    } 
    
  // ChannelAdapter為系統與系統間的端點,將這端點的輸出通道設為上面的outputChannel,我們將可以利用該通道取得資料

    @Bean
    public UnicastReceivingChannelAdapter udpInboundAdapter() {
        UnicastReceivingChannelAdapter unicastReceivingChannelAdapter = new UnicastReceivingChannelAdapter(1234);
        unicastReceivingChannelAdapter.setOutputChannel(outputChannel());
        return unicastReceivingChannelAdapter;
    }
    
  // Integration 流程

  // 1. 接收從outputChannel出來的資料

  // 2. 將資料格式轉換為字串

  // 3. 格式轉換完後,將資料帶入一個MessageHandler(這邊就是剛剛第一個設定的Bean)

    @Bean
    public IntegrationFlow integrationFlow() {
        return IntegrationFlows
                .from(udpInboundAdapter())
                .transform(Transformers.objectToString())
                .handle(udpMessageHandler())
                .get();
    }
    
}

MessageHandler

MessageHandler將會接收某一個Channel所送來的資料,我們可以接處理訊息的邏輯寫在此。

public class UdpMessageHandler implements MessageHandler {

    private static Logger logger = LogManager.getLogger(UdpMessageHandler.class);
    
    @Override
    public void handleMessage(Message<?> message) throws MessagingException {     
        logger.info(message.getPayload());
    }
    
}

Socket

將該Client跑起來後,對Console進行輸入,Enter後即可輸出資料流。

public class UdpClient {

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        
        @SuppressWarnings("resource")
        DatagramSocket client = new DatagramSocket();
        InetAddress inetAddress = InetAddress.getByName("localhost");
        
        String sentence = in.readLine();
        byte[] sendData = sentence.getBytes();
        
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, inetAddress, 1234);
        client.send(sendPacket);
    }
    
}

App

這邊就是我們的Server端

public class UdpApp {

    public static void main(String[] args) {
        @SuppressWarnings("resource")
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(UdpIntegrationConfig.class);
        context.start();
    }
  
}

Result

直接上圖,上面是Client端,反之下麵則是我們的Sever,我傳送一句Hello過去,可以看到Integration Server即收到該字串。

Refenrence

WIKI - 企業應用整合
EAI技術和概念解析

← [Java] SQL Exception Handle [Spring MVC] CH 4.2 Spring Integration TCP →
 
comments powered by Disqus