agumaster_crawler系統負責啓動爬蟲取得數據,以後便往隊列中推送.spring
agumaster_crawler系統中pom.xml關於RabbitMq的依賴是:網絡
<!-- RabbitMq --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
agumaster_crawler系統中application.properties文件裏對於RabbitMq的設置是:app
spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
以後,就能夠把Sender類寫出來:函數
package com.heyang.agumasterCrawler; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class Sender { @Autowired private AmqpTemplate mqTlt; public void send(String msg) { this.mqTlt.convertAndSend("stockQueue",msg); } }
具體使用Sender類的JUnit測試函數:spring-boot
@SpringBootTest class AgumasterCrawlerApplicationTests { @Autowired private Sender sender; @Test void contextLoads() throws Exception { BaseCrawler crawler=new FenghuangCrawler(); List<Stock> stockList=crawler.getStockList(); ObjectMapper mapper = new ObjectMapper(); for(Stock s:stockList) { String str=mapper.writeValueAsString(s); this.sender.send(str); } } }
發送給完畢後,RabbitMq隊列的狀況:測試
三千八百零一支股票都送到了.this
而原有Agumaster系統中,也要添加RabbitMq的依賴,spa
<!-- RabbitMq --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
這個和上面的是同樣的.code
以後就能夠寫接收類了:xml
package com.ufo.hy.agumaster.mq; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; /** * Used to receive stock code/names * @author Heyang * */ @Component @RabbitListener(queues="stockQueue") public class Receiver { @RabbitHandler public void QueueReceive(String receivedMsg) { System.out.println(receivedMsg); } }
這個類在工程Agumaster啓動後便會去隊列裏取得消息回來,下面是部分它取得的消息:
{"id":2790,"code":"600538","name":"國發股份","utime":null,"src":null,"ctime":null} {"id":2791,"code":"002367","name":"康力電梯","utime":null,"src":null,"ctime":null} {"id":2792,"code":"600410","name":"華勝天成","utime":null,"src":null,"ctime":null} {"id":2793,"code":"601007","name":"金陵飯店","utime":null,"src":null,"ctime":null} {"id":2794,"code":"603955","name":"大千生態","utime":null,"src":null,"ctime":null} {"id":2795,"code":"300227","name":"光韻達","utime":null,"src":null,"ctime":null} {"id":2796,"code":"603195","name":"公牛集團","utime":null,"src":null,"ctime":null} {"id":2797,"code":"000726","name":"魯 泰A","utime":null,"src":null,"ctime":null} {"id":2798,"code":"002013","name":"中航機電","utime":null,"src":null,"ctime":null} {"id":2799,"code":"002868","name":"綠康生化","utime":null,"src":null,"ctime":null} {"id":2800,"code":"002558","name":"巨人網絡","utime":null,"src":null,"ctime":null} {"id":2801,"code":"002391","name":"長青股份","utime":null,"src":null,"ctime":null} {"id":2802,"code":"300010","name":"立思辰","utime":null,"src":null,"ctime":null} {"id":2803,"code":"000902","name":"新洋豐","utime":null,"src":null,"ctime":null} {"id":2804,"code":"601965","name":"中國汽研","utime":null,"src":null,"ctime":null} {"id":2805,"code":"300171","name":"東富龍","utime":null,"src":null,"ctime":null} {"id":2806,"code":"300406","name":"九強生物","utime":null,"src":null,"ctime":null} {"id":2807,"code":"600857","name":"寧波中百","utime":null,"src":null,"ctime":null} {"id":2808,"code":"002463","name":"滬電股份","utime":null,"src":null,"ctime":null} {"id":2809,"code":"002560","name":"通達股份","utime":null,"src":null,"ctime":null} ....
這樣作,就用消息系統完成了系統的部分解耦.
--2020年5月9日--