ActiveMQ开发实例2

消息生产者
package activemq.yang;


//ConnectionFactory 是连接工厂,负责创建Connection。 
//Connection 负责创建 Session。
//Session 创建 MessageProducer(用来发消息) 和 MessageConsumer(用来接收消息)。
//Destination 是消息的目的地。
import javax.jms.Connection;   
import javax.jms.DeliveryMode;   
import javax.jms.Destination;   
import javax.jms.JMSException;   
import javax.jms.MessageProducer;   
import javax.jms.Session;   
import javax.jms.TextMessage;   
  
import org.apache.activemq.ActiveMQConnection;   
import org.apache.activemq.ActiveMQConnectionFactory;   
  
public class Producer {   
  
    private String user = ActiveMQConnection.DEFAULT_USER;   
  
    private String password = ActiveMQConnection.DEFAULT_PASSWORD;   
  
    private String url = "tcp://localhost:61616";   
  
    private String subject = "QDEFAULT2";   
  
    private Destination destination = null;   
  
    private Connection connection = null;   
  
    private Session session = null;   
  
    private MessageProducer producer = null;   
    
	private static final int SEND_NUMBER = 5;
	
  
    // 初始化   
    private void initialize() throws JMSException, Exception {   
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);   
        connection = connectionFactory.createConnection();   
        connection.start();  
        session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);   
        destination = session.createQueue(subject);   
        producer = session.createProducer(destination);   
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);   

    }   
  
    // 发送消息   
    public void produceMessage() throws JMSException, Exception {   
        initialize();   
        String text = "Hello World";
        for (int i = 0; i < SEND_NUMBER; i++){
            TextMessage msg = session.createTextMessage(text + i);
            System.out.println("Producer:->Sending message: " + text + i);   
            this.producer.send(msg);   
        }
        session.commit();
        System.out.println("Producer:->Message sent complete!");   
    }   
  
    // 关闭连接   
    public void close() throws JMSException {   
     
        if (producer != null)   
            producer.close();   
        if (session != null)   
            session.close();   
        if (connection != null)   
            connection.close();    
        System.out.println("Producer:->Closing connection"); 
    }   
}    

消息消费者

package activemq.yang;

import javax.jms.Connection;   
import javax.jms.Destination;   
import javax.jms.JMSException;   
import javax.jms.MessageConsumer;   
import javax.jms.Session;   
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;   
import org.apache.activemq.ActiveMQConnectionFactory;   
  
public class Consumer {   
  
    private String user = ActiveMQConnection.DEFAULT_USER;   
  
    private String password = ActiveMQConnection.DEFAULT_PASSWORD;   
  
    private String url = "tcp://localhost:61616";   
  
    private String subject = "QDEFAULT2";   
  
    private Destination destination = null;   
  
    private Connection connection = null;   
  
    private Session session = null;   
  
    private MessageConsumer consumer = null;   
  
    // 初始化   
    private void initialize() throws JMSException, Exception {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url); 
        connection = connectionFactory.createConnection();   
        connection.start();   
        session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);  
        destination = session.createQueue(subject); 
        consumer = session.createConsumer(destination); 
       
    }   
  
    // 消费消息   
    public void consumeMessage() throws JMSException, Exception {   
        initialize();   
        System.out.println("Consumer:->Begin Receive...");  
        while (true) {
			TextMessage message = (TextMessage) consumer.receive(1000);
			if (null != message) {
				System.out.println("Consumer:->Receiving message: " + message.getText());
			} else {
				System.out.println("Consumer:->Receive completed");  
				break;

			}
		}
	}
  
    // 关闭连接   
    public void close() throws JMSException {   
        if (consumer != null)   
            consumer.close();   
        if (session != null)   
            session.close();   
        if (connection != null)   
            connection.close();   
        System.out.println("Consumer:->Closing connection");  
    }


 

}

测试类

package activemq.yang;

import javax.jms.JMSException;


public class Test {

	/**
	 * @param args
	 */

	public static void main(String[] args) throws JMSException, Exception {


		Consumer consumer = new Consumer();
		Producer producer = new Producer();

		producer.produceMessage();
		producer.close();

		consumer.consumeMessage();
		consumer.close();

	}
}
运行Test

监听界面:

因为设置session的的属性值为true,所以消息继续存在,改成false,再次执行





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值