Java

spring integration 入门例子 Filter

Submitted by Lizhe on Thu, 06/29/2017 - 15:29

 

使用filter可以有效过滤非法或者不想要的数据, filter方法返回一个布尔类型的结果, 如果结果是true,消息将继续被传递下去, 如果是false, 消息会被丢弃

我们仍然使用一个输出来进行测试

输出的接口

package name.lizhe.filter;

public interface HelloService {
    void sayHello(String name);
}

输出的实现类

package name.lizhe.filter;

public class MyHelloService implements HelloService {

spring integration 入门例子 File Channel Adapter

Submitted by Lizhe on Wed, 06/28/2017 - 11:30

如果你使用过camel你一定记得这样的用法

copy文件从一个文件夹到另一个文件夹(不过camel会将input文件夹中处理过的文件移动到input/.camel文件夹)

package lzcamelsample;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

spring integration 入门例子 Channel Adapter

Submitted by Lizhe on Wed, 06/28/2017 - 10:28
A Channel Adapter is a Message Endpoint that enables connecting a single sender or receiver to a Message Channel. Spring Integration provides a number of adapters out of the box to support various transports, such as JMS, File, HTTP, Web Services, and Mail. Those will be discussed in upcoming chapters of this reference guide. However, this chapter focuses on the simple but flexible Method-invoking Channel Adapter support.

spring integration 入门例子 splitter

Submitted by Lizhe on Tue, 06/27/2017 - 17:35

splitter 并不是对单一消息的广播, 而是用来把消息内容进行切割

例如下面的例子里我将把消息内容 "a,b,c,d,e,f" 拆分成分别含有"a","b","c","e","f"的五条消息

首先创建一个输入内容用的接口

package name.lizhe.splitter;

public interface SpeakerService {
    void sayHello(String name);
}
 

然后是这个接口的实现类, 内容很简单,只是输入传入的消息为了我们的测试

package name.lizhe.splitter;

RabbitMQ Exchange(交换器)类型

Submitted by Lizhe on Fri, 06/23/2017 - 10:09

之前的例子里我使用了direct交换器

direct交换器在使用默认空白字符串作为名称时会自动绑定到默认交换器,然后将路由键(routing key)作为队列名称

在之前的例子中你已经见过了

当然你也可以给它其他名称, 但是记得你需要自己绑定交换器和队列

Channel channel = connection.createChannel();
channel.exchangeDeclare("myexchange", "direct");

channel.queueDeclare(queueName, false, false, true, null);
channel.queueBind(queueName, exchangeName, routingKey);  

Rabbitmq一共有四种类型的交换器

direct, fanout,topic和headers

RabbitMQ 读取方式

Submitted by Lizhe on Thu, 06/22/2017 - 17:31

 

channel.basicConsume(queueName, true, consumer);

会基于一种 发布-订阅 的模型来消费队列中的消息, 即消息一到达, 消费者就开始消费

GetResponse response = channel.basicGet(queueName, true);

则基于request模型, 每次只取回一个消息, 然后进行消费

来看下面的例子

首先我会使用一段java代码来初始化一个queue, 但是不绑定消费者

package name.lizhe.tool;

import java.io.IOException;
import java.util.concurrent.TimeoutException;