ActiveMq是Apache提供的開源消息系統採用java實現,java
很好地支持JMS(Java Message Service,即Java消息服務) 規範面試
ActiveMq安裝:http://activemq.apache.org/co... 在官網下載安裝對應的版本spring
下載完成後解壓就可使用docker
ActiveMq默認的端口號是8161,用戶名和密碼都是admin 在本機可使用http://localhost:8161 去訪問apache
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency>
spring.activemq.broker-url=tcp://localhost:61616 #若是是點對點(queue),那麼此處默認應該是false,若是發佈訂閱,那麼必定設置爲true spring.activemq.packages.trust-all=true spring.activemq.user=admin spring.activemq.password=admin
@Component public class QueueBean{ //建立一個隊列實例 @Bean Queue queue(){ //這裏設置的消息是隊列的名稱 return new ActiveMQQueue("hello.javaboy"); } }
@Component public class JmsComponent{ //springboot提供的消息模板 @Autowired JmsMessagingTemplate jmsMessagingTemplate; //本身建立的隊列實例 @Autowired Queue queue; /** * 發送消息 * @param message */ public void send(Message message){ jmsMessagingTemplate.convertAndSend(this.queue,message); } /** * 接收消息 * @param message */ //表示監聽該隊列名稱發來的消息 @JmsListener(destination = "hello.javaboy") public void readMessage(Message message){ System.out.println(message); } }
public class Message implements Serializable { private String content;//消息主體 private Date sendDate;//消息發送的時間 //省略get、set、tostring方法 }
在測試類中注入JmsComponent 調用send()方法進行消息的轉發springboot
@SpringBootTest class ActivemqApplicationTests { @Autowired JmsComponent jmsComponent; @Test void contextLoads() { Message message = new Message(); message.setContent("hello activeMq"); message.setSendDate(new Date()); jmsComponent.send(message); } }
首先啓動項目,在運行測試類進行消息發送:tcp
控制檯會打印消息內容:
spring-boot
rabbitmq安裝比較繁瑣,這裏使用docker容器進行安裝,docker安裝很是方便,一條命令所有搞定測試
經過docker安裝rabbitmqui
-P(大p)表示自動映射到主機端口
docker run -d --hostname my-rabbitmq --name some-rabbitmq -P rabbitmq:3-management
首先導入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
編寫配置文件:
#配置rabbitMQ spring.rabbitmq.host=localhost spring.rabbitmq.username=guest spring.rabbitmq.password=guest spring.rabbitmq.port=32771
直連交換機:Direct exchange
扇形交換機:Fanout exchange
主體交換機:Topic exchange
首部交換機:Headers exchange
下面分別介紹4中交換模式:
//Direct策略(只轉發給routingKey相匹配的用戶) @Configuration public class RabbitDirectConfig { public final static String DIRECTNAME = "javaboy-direct"; //消息隊列 @Bean Queue queue(){ //name值爲隊列名稱,routingKey會與他進行匹配 return new Queue("hello.RabbitMQ"); } @Bean Queue queue1(){ return new Queue("hello.RabbitMQ1"); } @Bean DirectExchange directExchange(){ //第一個參數爲DIRECTNAME、第二個參數表示重啓後是否有效,第三參數表示長時間未使用是否刪除 return new DirectExchange(DIRECTNAME,true,false); } @Bean Binding binding(){ //將隊列queue和DirectExchange綁定在一塊兒 return BindingBuilder.bind(queue()).to(directExchange()).with("direct"); } @Bean Binding binding1(){ //將隊列queue和DirectExchange綁定在一塊兒 return BindingBuilder.bind(queue1()).to(directExchange()).with("direct"); } }
//配置消費者 @Component public class DirectReceiver { //只監聽queue()隊列的消息 @RabbitListener(queues = "hello.RabbitMQ") public void hanlder(String msg){ System.out.println("hanlder>>>"+msg); } //只監聽queue1()隊列的消息 @RabbitListener(queues = "hello.RabbitMQ1") public void hanlder1(String msg){ System.out.println("hanlder1>>>"+msg); } }
測試代碼:
在springboot的測試類中注入RabbitTemplate(springboot提供的RabbitMQ模板)
@Autowired RabbitTemplate rabbitTemplate; @Test void contextLoads() { //兩個參數第一個是routingKey、第二個爲消息內容 rabbitTemplate.convertAndSend("hello.RabbitMQ","hello RabbitMQ test"); rabbitTemplate.convertAndSend("hello.RabbitMQ1","hello RabbitMQ test222"); }
啓動項目後,運行測試類能夠看到只有與routingkey相匹配的消費者受到了對應的消息:
二、Fanout exchange
//Fanout策略(只要是與他綁定的隊列,都會收到消息與routingKey無關) @Configuration public class RabbitFanoutConfig { public final static String FANOUTNAME = "javaboy-fanout"; //配置了兩個消息隊列queueOne和queueTwo @Bean Queue queueOne(){ return new Queue("queue-one"); } @Bean Queue queueTwo(){ return new Queue("queue-two"); } @Bean FanoutExchange fanoutExchange(){ return new FanoutExchange(FANOUTNAME,true,false); } //將兩個隊列與FanoutExchange綁定 @Bean Binding bindingOne(){ return BindingBuilder.bind(queueOne()).to(fanoutExchange()); } @Bean Binding bindingTwo(){ return BindingBuilder.bind(queueTwo()).to(fanoutExchange()); } }
//配置消費者 @Component public class FanoutReceiver { //兩個消費者分別監聽兩個不一樣的隊列 @RabbitListener(queues = "queue-one") public void hanlder1(String msg){ System.out.println("FanoutReceiver:hanlder1>>>"+msg); } @RabbitListener(queues = "queue-two") public void hanlder2(String msg){ System.out.println("FanoutReceiver:hanlder2>>>"+msg); } }
@Test void rabbitFanout(){ //三個參數表示RabbitFanoutConfig的名稱、routingkey、消息內容 rabbitTemplate.convertAndSend(RabbitFanoutConfig.FANOUTNAME,null,"hello fanout test"); }
該方式與routingkey無關全部寫null便可
查看輸出:能夠看到兩個消費者都收到了消息
topic策略能夠根據routingKey的規則(通配符方式)進行去匹配隊列進行轉發規則爲.#. *爲單詞,#表示模糊匹配
例如routingkey爲:xiaomi.# 那麼帶有xiaomi.開頭的隊列都會收到該消息
routingkey爲:#.phone.# 表示消息的routingKey中帶有phone時 就會去匹配帶有phone的隊列
/topic策略能夠根據routingKey的規則(通配符方式)進行去匹配隊列進行轉發規則爲*.#.* //*爲單詞,#表示模糊匹配 @Configuration public class RabbitTopicConfig { public final static String TOPICNAME = "javaboy-topic"; @Bean TopicExchange topicExchange(){ return new TopicExchange(TOPICNAME,true,false); } @Bean Queue xiaomi(){ return new Queue("xiaomi"); } @Bean Queue huawei(){ return new Queue("huawei"); } @Bean Queue phone(){ return new Queue("phone"); } @Bean Binding xiaomiBinding(){ //xiaomi.#:表示消息的routingKey是以xiaomi開頭的就會路由到xiaomi的隊列 return BindingBuilder.bind(xiaomi()).to(topicExchange()).with("xiaomi.#"); } @Bean Binding huaweiBinding(){ return BindingBuilder.bind(huawei()).to(topicExchange()).with("huawei.#"); } @Bean Binding phoneBinding(){ //#.phone.#:表示消息的routingKey中帶phone的都會路由到phone的隊列 return BindingBuilder.bind(phone()).to(topicExchange()).with("#.phone.#"); } }
@Component public class TopicReceiver { //分別監聽名稱爲xiaomi、huawei、phone的隊列 @RabbitListener(queues = "xiaomi") public void handlerXM(String msg){ System.out.println("TopicReceiver:handlerXM>>>"+msg); } @RabbitListener(queues = "huawei") public void handlerHW(String msg){ System.out.println("TopicReceiver:handlerHW>>>"+msg); } @RabbitListener(queues = "phone") public void handlerPHONE(String msg){ System.out.println("TopicReceiver:handlerPHONE>>>"+msg); } }
@Test void rabbitTopic(){ //根據匹配規則該消息只能被xiaomi的隊列收到 rabbitTemplate.convertAndSend(RabbitTopicConfig.TOPICNAME,"xiaomi.news","小米新聞"); //根據匹配規則該消息只能被phone的隊列收到 rabbitTemplate.convertAndSend(RabbitTopicConfig.TOPICNAME,"vivo.phone","vivo手機"); //根據匹配規則該消息能夠別huawei和phone兩個隊列收到 rabbitTemplate.convertAndSend(RabbitTopicConfig.TOPICNAME,"huawei.phone","華爲手機"); }
查看輸出:
能夠看到routingkey爲huawei.phone的消息匹配了兩個隊列,其餘兩個都只匹配了一個隊列
該模式是根據路由規則的header進行匹配的,在進行匹配的時候須要傳入一個map集合,routingkey去匹配map便可中的key value,匹配規則可使any或者all,any表示只要包含任意信息就能夠,all表示全部信息都必須匹配
@Configuration public class RabbitHeaderConfig { public final static String HEADERNAME = "javaboy-header"; @Bean HeadersExchange headersExchange(){ return new HeadersExchange(HEADERNAME,true,false); } //分別建立兩個不一樣header的隊列 @Bean Queue queueName(){ return new Queue("name-queue"); } @Bean Queue queueAge(){ return new Queue("age-queue"); } @Bean Binding bindingName(){ Map<String,Object> map = new HashMap<>(); map.put("name","hello"); //表示若是routingKey匹配的map集合中的key value 就會將消息轉發到對應的路由上 return BindingBuilder.bind(queueName()).to(headersExchange()).whereAny(map).match(); } @Bean Binding bindingAge(){ return BindingBuilder.bind(queueAge()).to(headersExchange()).where("age").exists(); } }
@Component public class HeaderReceiver { @RabbitListener(queues = "name-queue") public void handlerName(byte[] msg){ System.out.println("HeaderReceiver:handlerName>>>>"+new String(msg,0,msg.length)); } @RabbitListener(queues = "age-queue") public void handlerAge(byte[] msg){ System.out.println("HeaderReceiver:handlerAge>>>>"+new String(msg,0,msg.length)); } }
@Test public void rabbitHeader(){ //設置消息,而且設置header,setHeader("name","hello")分別表示map集合中的key、value Message nameMessage = MessageBuilder.withBody("hello name".getBytes()).setHeader("name","hello").build(); Message ageMessage = MessageBuilder.withBody("hello 99 age".getBytes()).setHeader("age","99").build(); rabbitTemplate.send(RabbitHeaderConfig.HEADERNAME,null,nameMessage); rabbitTemplate.send(RabbitHeaderConfig.HEADERNAME,null,ageMessage); }
查看輸出:
改變setheader中的值查看結果:
Message nameMessage = MessageBuilder.withBody("hello name".getBytes()).setHeader("name","javaboy").build();
能夠看到由於key、value匹配不上只打印了一條消息。
你們看完有什麼不懂的能夠在下方留言討論,也能夠關注我私信問我,我看到後都會回答的。也歡迎你們關注個人公衆號:前程有光,金三銀四跳槽面試季,整理了1000多道將近500多頁pdf文檔的Java面試題資料,文章都會在裏面更新,整理的資料也會放在裏面。謝謝你的觀看,以爲文章對你有幫助的話記得關注我點個贊支持一下!