欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ActiveMQ入门例子简单易懂

程序员文章站 2022-03-24 12:05:30
...
首先下载ActiveMQ:http://activemq.apache.org/download.html
在windows下启动服务:activemq.bat start
然后进入admin管理页面:http://127.0.0.1:8161/admin/
账号和密码为:admin/admin
账号可以配置的在conf/users.properties



import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


public class MQSession {
    //ActiveMq 的默认用户名
    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
    //ActiveMq 的默认登录密码
    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
    //ActiveMQ 的链接地址
    private static final String BROKEN_URL = ActiveMQConnection.DEFAULT_BROKER_URL;
    
    public static Session getSession()
    {
    	Session createSession  = null;
    	try {
			ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEN_URL);
			Connection createConnection = activeMQConnectionFactory.createConnection();
			createConnection.start();
			createSession = createConnection.createSession(true, Session.SESSION_TRANSACTED);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	return createSession;
    }
}




import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;


public class ServerActiveMQ {

	private Session session;
	public ServerActiveMQ()
	{
		session = MQSession.getSession();
	}
	
	public void sendMsg(String queueName)
	{
		try {
			MessageProducer createProducer = session.createProducer(session.createQueue(queueName));
			for(int i=1;i<=100;i++)
			{
				String msg = "ServerActiveMQ send:"+i+"次";
				System.out.println(msg);
				createProducer.send(session.createTextMessage(msg));
				session.commit();
			}
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new ServerActiveMQ().sendMsg("test");

	}

}






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


public class ClientActiveMQ {

	private Session session;
	
	public ClientActiveMQ()
	{
		session = MQSession.getSession();
	}
	
	public void getMessage(String queueName){
		try {
			MessageConsumer createConsumer = session.createConsumer(session.createQueue(queueName));
			while(true)
			{
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				TextMessage msg = (TextMessage)createConsumer.receive();
				if(null != msg)
				{
					msg.acknowledge();
					System.out.println("接受服务发送的内容:"+msg.getText());
				}
			}
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		
		new ClientActiveMQ().getMessage("test");
	}

}




在管理页面http://127.0.0.1:8161/admin/ 点击Queues可以看到相关队列信息。
服务和客户端我这里定义的Queue是test,所以在此通道下数据得以交互


相关标签: activemq