Spring Integration-File Adapter使用

File Adapter主要应用于企业应用程序之间共享文件系统的情况,一个应用写入,而其他应用通过轮询文件系统进行读取。File Adapter从不同的文件系统提取文件再转变成框架的Message并发布至通道中,或者从通道中提取Message再转变为文件并写入文件系统中。

 

一、配置环境

1.下载Spring Integration

http://s3.amazonaws.com/dist.springframework.org/release/INT/spring-integration-2.1.0.RELEASE-dist.zip

 

2.配置classpath依赖的jar

 

spring-integration-core-2.1.0.RELEASE.jar 

spring-integration-file-2.1.0.RELEASE.jar

org.springframework.asm-3.1.0.RELEASE.jar

org.springframework.aop-3.1.0.RELEASE.jar

org.springframework.beans-3.1.0.RELEASE.jar

org.springframework.core-3.1.0.RELEASE.jar

org.springframework.context-3.1.0.RELEASE.jar

org.springframework.context.support-3.1.0.RELEASE.jar

org.springframework.expression-3.1.0.RELEASE.jar

aopalliance-1.0.jar

commons-logging-1.1.1.jar

 

二、XML配置文件:spring-integration-file-context.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
	xmlns:int-file="http://www.springframework.org/schema/integration/file"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	    http://www.springframework.org/schema/integration 
	    http://www.springframework.org/schema/integration/spring-integration-2.1.xsd
        http://www.springframework.org/schema/integration/file
		http://www.springframework.org/schema/integration/file/spring-integration-file-2.1.xsd">
	<!-- 定义通道 -->
	<int:channel id="file-channel"/>
	
	<!-- 文件类型-输入通道适配器 -->
	<int-file:inbound-channel-adapter
		directory="D:/spring-integration-samples/input" channel="file-channel">
	    <int:poller fixed-rate="1000"/>
	</int-file:inbound-channel-adapter>
	
	<!-- 文件类型-输出通道适配器 -->
	<int-file:outbound-channel-adapter directory="D:/spring-integration-samples/output" channel="file-channel"/>
</beans>

 

三、编写测试类

 

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestFileSupport {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
		"spring-integration-file-context.xml");
	}
}

 

四、示例说明

实现功能:每秒间隔轮询D:/spring-integration-samples/input目录,如果发现该目录有文件,就依次复制到D:/spring-integration-samples/output目录中。实际上就是文件copy的功能

该示例使用了Spring Integration提供的File适配器。

 

深入File Adapter

1.防止重复读取:prevent-duplicates="true"

 

<int-file:inbound-channel-adapter
        directory="D:/spring-integration-samples/input" 
        channel="file-channel" prevent-duplicates="true">

 

2.文件过滤:filename-pattern="*.doc"或者filename-regex="[ABC]_positions.doc"

<int-file:inbound-channel-adapter
        directory="D:/spring-integration-samples/input" 
        channel="file-channel" filename-pattern="*.doc">
或者
<int-file:inbound-channel-adapter
        directory="D:/spring-integration-samples/input" 
        channel="file-channel" filename-regex="[ABC]_positions.doc">

 

 

3.文件锁:<file:nio-locker/>

<int-file:inbound-channel-adapter
        directory="D:/spring-integration-samples/input" 
        channel="file-channel" filename-regex="[ABC]_positions.doc">
    <file:nio-locker/>
    ……
</int-file:inbound-channel-adapter>

 

4.删除源文件:delete-source-files="true"

 

<int-file:outbound-channel-adapter 
        directory="D:/spring-integration-samples/output" 
        channel="file-channel" delete-source-files="true"/>

 

实际上还有很多属性用于设置共享文件系统出现的各种情况。这里不再赘述。

Spring Boot provides easy integration with the Spring Integration framework for implementing messaging and integration patterns. Spring Integration provides support for various messaging protocols, including MQTT, a lightweight publish/subscribe messaging protocol. To integrate Spring Integration with MQTT in Spring Boot, follow these steps: 1. Add the following dependencies to your `pom.xml` file: ``` <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-core</artifactId> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-mqtt</artifactId> </dependency> ``` 2. Create a configuration class for MQTT integration: ``` @Configuration @EnableIntegration public class MqttIntegrationConfig { @Value("${mqtt.broker.url}") private String brokerUrl; @Value("${mqtt.client.id}") private String clientId; @Bean public MessageChannel mqttInputChannel() { return new DirectChannel(); } @Bean public MqttPahoClientFactory mqttClientFactory() { DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory(); MqttConnectOptions options = new MqttConnectOptions(); options.setServerURIs(new String[] { brokerUrl }); factory.setConnectionOptions(options); return factory; } @Bean public MessageProducer inbound() { MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter( clientId, mqttClientFactory(), "topic1", "topic2"); adapter.setCompletionTimeout(5000); adapter.setConverter(new DefaultPahoMessageConverter()); adapter.setQos(1); return adapter; } @Bean public MessageHandler mqttMessageHandler() { return new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { System.out.println("Received MQTT message: " + message.getPayload()); } }; } @Bean public IntegrationFlow mqttInFlow() { return IntegrationFlows.from(inbound()) .handle(mqttMessageHandler()) .channel(mqttInputChannel()) .get(); } } ``` 3. Configure the MQTT broker URL and client ID in your application properties file: ``` mqtt.broker.url=tcp://localhost:1883 mqtt.client.id=myClientId ``` 4. Use the `mqttInputChannel` channel to send messages to the MQTT broker. For example: ``` @Autowired private MessageChannel mqttInputChannel; public void sendMqttMessage(String payload) { mqttInputChannel.send(MessageBuilder.withPayload(payload).build()); } ``` With these steps, you can easily integrate MQTT messaging with your Spring Boot application using Spring Integration.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值