email集成流:
@Data
// 使用了 @ConfigurationProperties 注解,可以在 application.yml 配置文件中配置
@ConfigurationProperties(prefix="springbootlearn.email")
@Component
public class EmailProperties {
private String username;
private String password;
private String host;
private String mailbox;
private long pollRate = 60000; // 60秒轮询一次
// 产生一个 IMAP URL
public String getImapUrl() {
return String.format("imaps://%s:%s@%s/%s",
this.username, this.password, this.host, this.mailbox);
}
}
在application.yml定义:
springbootlearn:
email:
host: imap.springbootlearndemo.com
mailbox: INBOX
username: demo-in-flow
password: 123456
poll-rate: 60000
Java DSL 配置:
@Configuration
public class OrderEmailIntegrationConfig {
@Bean
public IntegrationFlow orderEmailFlow(
EmailProperties emailProps,
// 将电子邮件转换为订单对象的转换器
EmailToOrderTransformer emailToOrderTransformer,
OrderSubmitMessageHandler orderSubmitHandler) {
return IntegrationFlows
.from(
// IMAP 电子邮件入站信道适配器, IMP URL 来创建通道适配器
Mail.imapInboundAdapter(emailProps.getImapUrl()),
e -> e.poller(
Pollers.fixedDelay(emailProps.getPollRate())))
.transform(emailToOrderTransformer)
// 处理程序接收一个订单对象,并将其提交到REST API
.handle(orderSubmitHandler)
.get();
}
}
maven依赖:
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-file</artifactId>
</dependency>
实现 Transformer 接口
// 继承AbstractMailMessageTransformer,实现 Transformer 接口
@Component
public class EmailToOrderTransformer extends AbstractMailMessageTransformer<Order> {
// 从到来的消息中将邮件信息提取到
@Override
protected AbstractIntegrationMessageBuilder<Order>
doTransform(Message mailMessage) throws Exception {
Order myorder = processPayload(mailMessage);
return MessageBuilder.withPayload(myorder);
}
// ...
}
上面Order是自定义的类,如:
package spirngbootlearn.email;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
@Data
public class Order {
private final String email;
private List<Pen> pens = new ArrayList<>();
public void addPen(Pen pen) {
this.pens.add(pen);
}
}
package spirngbootlearn.email;
import java.util.Map;
import org.springframework.integration.handler.GenericHandler;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class OrderSubmitMessageHandler
implements GenericHandler<Order> {
private RestTemplate rest;
// ApiProperties 是为了避免在调用 postForObject() 方法时对 URL 进行硬编码。
private ApiProperties apiProps;
public OrderSubmitMessageHandler(
ApiProperties apiProps, RestTemplate rest) {
this.apiProps = apiProps;
this.rest = rest;
}
@Override
public Object handle(Order order, Map<String, Object> headers) {
rest.postForObject(apiProps.getUrl(), order, String.class);
return null;
}
}
ApiProperties:
@Data
@ConfigurationProperties(prefix="spirngbootlearn.api")
@Component
public class ApiProperties {
private String url;
}
在 application.yml:
spirngbootlearn:
api:
url: http://api.spirngbootlearndemo.com
maven加入依赖:使RestTemplate可用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
这使得 RestTemplate 在 classpath 中可用,这也触发了 Spring MVC 的自动配置。作为一个独立的 Spring Integration 流,应用程序并不需要 Spring MVC,或是嵌入的Tomcat。
所以,在 application.yml
中禁用 Spring MVC 的自动配置:
spring:
main:
web-application-type: none # 重写为 none,使SpringMVC和Tomcat不会自动配置
spring.main.web-application-type 属性可以被设置为 servlet、reactive 或是 none。