省时间、直接上图了、废话少说。
首先就是要添加依赖了,在这里推荐大家一个hutool的工具类。相信你看过后会感兴趣的。
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <!-- amqp依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.1.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
#app
server: port: 8080 oms: client: node_to_local_queue: oms.declare node_to_local_topic: omsTopicExchange local_to_node_queue: oms.declare.result local_to_node_topic: omsTopicExchange node_to_local_key: key.oms.declare local_to_node_key: key.oms.declare.result msg_send_dir: C://DATA//RECEIVE// msg_receive_dir: C://DATA//RECEIVE//#spring spring: devtools: restart: enabled: false profiles: active: test logging: pattern: com.****.oms level: DEBUG file: ./logs/msg_client.log rabbitmq: host: localhost port: 5672 username: guest password: gue publisher-confirms: true publisher-returns: true template.mandatory: true virtual-host: oms listener: simple: concurrency: 2 max-concurrency: 2
上面这段是application.yml中的配置
spring boot配置类,作用为指定队列,交换器类型及绑定操作
import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitConfig { @Value("${oms.client.node_to_local_queue}") String node_to_local_queue; @Value("${oms.client.node_to_local_topic}") String node_to_local_topic; @Value("${oms.client.local_to_node_queue}") String local_to_node_queue; @Value("${oms.client.local_to_node_topic}") String local_to_node_topic; @Value("${oms.client.node_to_local_key}") String node_to_local_key; @Value("${oms.client.local_to_node_key}") String local_to_node_key; @Bean public Queue nodeToLocal() { return new Queue(node_to_local_queue, true); // true表示持久化该队列 } @Bean public Queue localToNode() { return new Queue(local_to_node_queue, true); } @Bean TopicExchange nodeToLocalExchange() { return new TopicExchange(node_to_local_topic); } @Bean TopicExchange localToNodeExchange() { return new TopicExchange(local_to_node_topic); } @Bean public Binding nodeToLocalBinding() { return BindingBuilder.bind(nodeToLocal()).to(nodeToLocalExchange()).with(node_to_local_key); } @Bean public Binding localToNodeBinding() { return BindingBuilder.bind(localToNode()).to(localToNodeExchange()).with(local_to_node_key); } }
生产者类
@Component public class Sender { private final static Log log = LogFactory.get(); @Value("${oms.client.node_to_local_queue}") String node_to_local_queue; @Value("${oms.client.node_to_local_topic}") String node_to_local_topic; @Value("${oms.client.local_to_node_queue}") String local_to_node_queue; @Value("${oms.client.local_to_node_topic}") String local_to_node_topic; @Value("${oms.client.node_to_local_key}") String node_to_local_key; @Value("${oms.client.local_to_node_key}") String local_to_node_key; @Autowired private RabbitTemplate rabbitTemplate; private RetryTemplate retryTemplate = new RetryTemplate(); /** * 发送消息,不需要实现任何接口,供外部调用 * * @param msg */ public void sendToNode(String msg, String filename) throws Exception { log.info("开始向node端发送消息,进程{},文件 :{} ", Thread.currentThread().getName(), filename); MessageProperties messageProperties = new MessageProperties(); messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN); Message message = new Message(msg.getBytes("UTF-8"), messageProperties); rabbitTemplate.setRetryTemplate(retryTemplate); rabbitTemplate.send(local_to_node_topic, local_to_node_key, message); log.info("结束向node端发送消息"); } }
下面就是读取文件的内容了、用到了上面提到的hutool工具(真的很好用)
@Component public class FileWatch { private static final cn.hutool.log.Log log = LogFactory.get(FileWatch.class); @Value("${oms.client.msg_receive_dir}") String watchDir; @Autowired Sender sender; @Scheduled(cron = "0 0/5 * * * ?",initialDelay = 60000*5)//每五分钟执行一次,第一启动延迟五分钟执行 void processLastFile() { if(!FileUtil.exist(watchDir)){ FileUtil.mkdir(watchDir); } log.info("处理监控文件夹:{}下面的所有文件。",watchDir); File[] files = FileUtil.ls(watchDir); log.info("共有待处理文件:{}个",files.length); for (File f:files) { if(f.getName().endsWith(".xml")){ try{ sender.sendToNode(FileUtil.readString(f,"UTF-8"),f.getName()); if(f.exists()){ f.delete(); } }catch (Exception e){ log.error("向node端发送失败,失败原因为{}",e.getLocalizedMessage()); } } } log.info("启动处理文件结束"); } public void watch(){ processLastFile(); File file = FileUtil.file(watchDir); log.info("启动文件监控:"+watchDir); Watcher watcher = new Watcher(){ @Override public void onCreate(WatchEvent<?> event, Path currentPath) { Object obj = event.context(); log.info("创建:{}-> {}", currentPath, obj); } @Override public void onModify(WatchEvent<?> event, Path currentPath) { Object obj = event.context(); log.info("修改:{}-> {}", currentPath, obj); try{ if(obj.toString().endsWith(".xml")){ File f = new File(currentPath.toString()+"\\"+obj.toString()); sender.sendToNode(FileUtil.readString(f,"UTF-8"),f.getName()); if(f.exists()){ FileUtil.del(f); } } }catch (Exception e){ log.error("向node端发送失败,失败原因为{}",e.getLocalizedMessage()); } } @Override public void onDelete(WatchEvent<?> event, Path currentPath) { Object obj = event.context(); log.info("删除:{}-> {}", currentPath, obj); } @Override public void onOverflow(WatchEvent<?> event, Path currentPath) { Object obj = event.context(); log.info("丢失:{}-> {}", currentPath, obj); } }; WatchMonitor watchMonitor = WatchMonitor.createAll(file,new DelayWatcher(watcher,1000)); watchMonitor.setMaxDepth(1); watchMonitor.start(); } }
public class FileWatchStartUp implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { FileWatch fileWatch = contextRefreshedEvent.getApplicationContext().getBean(FileWatch.class); fileWatch.watch(); } }
上面就是所有的代码、最后启动
@SpringBootApplication public class MsgclientApplication { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(MsgclientApplication.class); springApplication.addListeners(new FileWatchStartUp()); springApplication.run(args); } }
控制台可以看到对文件进行了监控、有新的文件进去后、读取、放入队列、消费、最后直接删除文件。