CPT402 Low Level Programming Lab 2

@[TOC](Java Message Service (JMS))

JMS API Programming Model

在这里插入图片描述

Point to point messaging system

  • Each message is addressed to a specific queue.
  • Queues retain all messages sent to them until the messages are consumed or expire.
  • Each message has only one consumer.
  • Connection -> createQueueConnection()

Publish/subscribe messaging system

  • Publishers and subscribers can dynamically publish or subscribe to a topic (queue).
  • Topics retain messages only as long as it takes to distribute them to subscribers.
  • Each message can have multiple consumers.
  • A client who subscribes to a topic can consume only messages sent after the client has created a subscription.
  • Connection -> createTopicConnection()

Synchronous fashion

  • A consumer explicitly fetches the message from the destination by calling the receive() method.
  • The receive() method can block until a message arrives or can time out if a message does not arrive within a specified time limit.

Asynchronous fashion

  • A client can register a message listener with a consumer.
  • Whenever a message arrives at the destination, the JMS provider delivers the message by calling the listener’s onMessage() method.

Low level program

package point2point;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Date;

public class synProducer {
    public static void main(String[] args) throws JMSException {
        // ConnectionFactory
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        // ConnectionFactory -> Connection
        Connection connection = factory.createQueueConnection();
        // Connection -> Session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

		// Session -> Destination
        Destination destination = session.createQueue("synQueue"); // point 2 point
//        Destination destination = session.createTopic("Publisher & Subscriber"); // Publisher / Subscriber
        
        // Session -> MessageProducer
        MessageProducer Producer = session.createProducer(destination);
		// Session -> Message
        Message message = session.createTextMessage("Text message: "+new Date(System.currentTimeMillis()));
        // public class Person implements Serializable (!A Serializable object)
//        ObjectMessage message = session.createObjectMessage(person);

        Producer.send(message);
        System.out.println( ((TextMessage) message).getText() );

        Producer.close();
        session.close();
        connection.close();

        System.out.println("Message send~");

    }
}

  • 写 Consumer
package point2point;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class synConsumer {
    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = factory.createQueueConnection();
        // Need start() in order to receive message, don't need when send.
        connection.start();

        Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue("synQueue");// point 2 point
//        Destination destination = session.createTopic("Publisher & Subscriber"); // Publisher / Subscriber
        MessageConsumer consumer = session.createConsumer(destination);

		//Synchronous
        Message message = consumer.receive(); 
        String text = ((TextMessage) message).getText();
        System.out.println("Receive message from ActiveMQ");
        System.out.println(text);
        
        // Asynchronous
//        consumer.setMessageListener(new MessageListener() {
//            @Override
//            public void onMessage(Message message) {
//                String text = ((TextMessage) message).getText();
//			      System.out.println("Receive message from ActiveMQ");
//			      System.out.println(text);
//            }
//        });
		// keep it running
//        System.out.println("Listener is working... Press any key + Enter to exit.");
//        Scanner scanner = new Scanner(System.in);
//        scanner.nextLine();

//	      consumer.setMessageListener(null);
        consumer.close();
        session.close();
        connection.close();

        System.out.println("Consumer complete~");
    }
}

  • Run activeMQ
    • Windows
      • [activemq_install_dir]\bin\activemq start
      • [activemq_install_dir]\bin\activemq stop
    • OS
      • cd [activemq_install_dir]/bin
      • ./activemq start (may take some time)
      • ./activemq stop
    • Access ActiveMQ console at:
      • http://127.0.0.1:8161/admin/ (username and password default:
        admin and admin)
  • Run Producer and Consumer
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值