ActiveMQ开发实例

启动ActiveMQ,直接进入安装目录执行\bin\activemq


如下,启动成功

访问http://localhost:8161/admin/queues.jsp


消息生产者:

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 Producer2 {

	/**
	 * @param args
	 */
	  private String user = ActiveMQConnection.DEFAULT_USER;   

	  private String password = ActiveMQConnection.DEFAULT_PASSWORD;   

	  private String url = "tcp://localhost:61616";   

	  private String subject = "QDEFAULT";   

	  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"); 
	  }      
	  
	  
	  
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Producer2  producer = new Producer2();
		try {
			producer.produceMessage();
			producer.close();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}


消息消费者:

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 Consumer2 {

	/**
	 * @param args
	 */
	
	private String user = ActiveMQConnection.DEFAULT_USER;   
	  
    private String password = ActiveMQConnection.DEFAULT_PASSWORD;   
  
    private String url = "tcp://localhost:61616";   
  
    private String subject = "QDEFAULT";   
  
    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(Boolean.FALSE.booleanValue(), 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");  
    }

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Consumer2 consumer = new Consumer2();
		try {
			consumer.consumeMessage();
			consumer.close();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		
		
	}

}


执行代码,http://localhost:8161/admin/queues.jsp显示如下:



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值