1. 程式人生 > 實用技巧 >Zookeeper學習(二)

Zookeeper學習(二)

ActiveMQ介紹

MQ是訊息中介軟體,是一種在分散式系統中應用程式藉以傳遞訊息的媒介,常用的有ActiveMQ,RabbitMQ,kafka。ActiveMQ是Apache下的開源專案,完全支援JMS1.1和J2EE1.4規範的JMS Provider實現。
特點:
1、支援多種語言編寫客戶端
2、對spring的支援,很容易和spring整合
3、支援多種傳輸協議:TCP,SSL,NIO,UDP等
4、支援AJAX
訊息形式:
1、點對點(queue)
2、一對多(topic)

ActiveMQ在windows上的安裝步驟:
訪問http://activemq.apache.org/ (ActiveMQ官網)
下載完成之後解壓壓縮包
配置JAVA_HOME變數,然後開啟bin目錄
雙擊activemq,執行
訪問http://localhost:8161/admin 使用者名稱和密碼都填寫admin
出現如下介面訪問成功

整合springboot

appliaction.properties配置

spring.activemq.broker-url=tcp://112.124.20.231:61616
# 表示可以傳送物件
spring.activemq.packages.trust-all=true
spring.activemq.user=admin
spring.activemq.password=admin

在啟動類中寫訊息佇列物件

package org.javaboy.activemq;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import javax.jms.Queue;


@SpringBootApplication
public class ActivemqApplication {

    public static void main(String[] args) {
        SpringApplication.run(ActivemqApplication.class, args);
    }

    @Bean
    Queue queue() {
        return new ActiveMQQueue("hello.javaboy");   //佇列名稱
    }
}

定義訊息收發的方法

package org.javaboy.activemq;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.Queue;

//定義訊息收發的方法
@Component
public class JmsComponent {
    @Autowired
    JmsMessagingTemplate jmsMessagingTemplate;  //訊息傳送模板
    @Autowired
    Queue queue;

    //定義方法傳送message
    public void send(Message message) {
        jmsMessagingTemplate.convertAndSend(this.queue,message);
    }

    //定義接受訊息
    @JmsListener(destination = "hello.javaboy")
    public void receive(Message message) {
        System.out.println(message);
    }
}

其中我們還需要Message訊息實體類

package org.javaboy.activemq;

import java.io.Serializable;
import java.util.Date;

public class Message implements Serializable {
    private String content;  //訊息主體
    private Date sendData;   //訊息傳送日期

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getSendData() {
        return sendData;
    }

    public void setSendData(Date sendData) {
        this.sendData = sendData;
    }

    @Override
    public String toString() {
        return "Message{" +
                "content='" + content + '\'' +
                ", sendData=" + sendData +
                '}';
    }
}

接下來定義一個測試類看看發出的訊息是否被收到

package org.javaboy.activemq;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivemqApplicationTests {

    @Autowired
    JmsComponent jmsComponent;

    @Test
    public void contextLoads() {
        Message message = new Message();
        message.setContent("hello javaboy");
        message.setSendData(new Date());
        jmsComponent.send(message);
    }

}

配置類中的接受方法接收到了傳送方的資訊(destination中的名稱和application.properties中寫的相同。)

控制檯列印成功

Message{content='hello javaboy', sendData=Tue Feb 11 11:37:22 GMT+08:00 2020}