activeMQ 學習筆記

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bry.jms</groupId>
    <artifactId>jms_test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>jms_test</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.9.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

1:隊列模式: 如果有多個消費者,生產者發送的消息會被隊列式接收,比如生產者發送4個消息,一共兩個消費者,那就是第一個消費者接收一個,第二個消費者接收一個,第一個消費者再接收一個,第二個消費者再接收一個,
生產者代碼:

public class AppProducer {
    private static final String url = "tcp://192.168.0.113:61616";
    private static final String queueName = "queue_test";

    public static void main(String[] args) throws JMSException {
        // 1.創建ConnectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        // 2.創建Connection
        Connection connection = connectionFactory.createConnection();
        // 3.啓動連接
        connection.start();
        // 4.創建會話 第一個參數是是否啓動事物處理,第二個參數設置了自動應答
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 5.創建一個目標
        Destination destination = session.createQueue(queueName);
        // 6.創建一個生產者
        MessageProducer producer = session.createProducer(destination);

        for (int i = 0; i < 100; i++) {
            //7.創建消息
            TextMessage textMessage = session.createTextMessage("test" + i);
            //8.發佈消息
            producer.send(textMessage);
            System.out.println("發送消息" + textMessage.getText());
        }

        //9.關閉連接
        connection.close();
    }
}

消費者:

public class AppConsumer {
    private static final String url = "tcp://192.168.0.113:61616";
    private static final String queueName = "queue_test";

    public static void main(String[] args) throws JMSException {
        // 1.創建ConnectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        // 2.創建Connection
        Connection connection = connectionFactory.createConnection();
        // 3.啓動連接
        connection.start();
        // 4.創建會話 第一個參數是是否啓動事物處理,第二個參數設置了自動應答
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 5.創建一個目標
        Destination destination = session.createQueue(queueName);
        // 6.創建一個消費者
        MessageConsumer consumer = session.createConsumer(destination);
        // 7.創建一個監聽器
        consumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                TextMessage textMessage = (javax.jms.TextMessage) message;
                try {
                    System.out.println("接收消息" + textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });

        // 8.關閉連接
        //connection.close();
    }
}

1:主題模式: 生產者發送消息,每個消費者會接收到全部消息,前提是消費者要在生產者沒有發送消息之前就監聽這個主題.
生產者代碼:

public class AppProducer {
    private static final String url = "tcp://192.168.0.113:61616";
    private static final String topicName = "topic_test";

    public static void main(String[] args) throws JMSException {
        // 1.創建ConnectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        // 2.創建Connection
        Connection connection = connectionFactory.createConnection();
        // 3.啓動連接
        connection.start();
        // 4.創建會話 第一個參數是是否啓動事物處理,第二個參數設置了自動應答
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 5.創建一個目標
        Destination destination = session.createTopic(topicName);
        // 6.創建一個生產者
        MessageProducer producer = session.createProducer(destination);

        for (int i = 0; i < 100; i++) {
            //7.創建消息
            TextMessage textMessage = session.createTextMessage("test" + i);
            //8.發佈消息
            producer.send(textMessage);
            System.out.println("發送消息" + textMessage.getText());
        }

        //9.關閉連接
        connection.close();
    }
}

消費者:

public class AppConsumer {
    private static final String url = "tcp://192.168.0.113:61616";
    private static final String topicName = "topic_test";

    public static void main(String[] args) throws JMSException {
        // 1.創建ConnectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        // 2.創建Connection
        Connection connection = connectionFactory.createConnection();
        // 3.啓動連接
        connection.start();
        // 4.創建會話 第一個參數是是否啓動事物處理,第二個參數設置了自動應答
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 5.創建一個目標
        Destination destination = session.createTopic(topicName);
        // 6.創建一個消費者
        MessageConsumer consumer = session.createConsumer(destination);
        // 7.創建一個監聽器
        consumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                TextMessage textMessage = (javax.jms.TextMessage) message;
                try {
                    System.out.println("接收消息" + textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });

        // 8.關閉連接
        //connection.close();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章