zoukankan      html  css  js  c++  java
  • ActiveMQ简单入门实例

    一、下载MQ

    官方网站下载:http://activemq.apache.org/
    我用的是 apache-activemq-5.15.0-bin

    二、安装

    我用的是64位所以双击 apache-activemq-5.15.0inwin64 下 activemq.bat

    登录 登陆:http://localhost:8161/admin/, 默认账户密码 admin = admin ,创建一个Queue,命名为FirstQueue

    三、环境

    创建项目 thomas

    导入apache-activemq-5.8.0lib目录  下的
    activemq-broker-5.8.0.jar
    activemq-client-5.8.0.jar
    geronimo-j2ee-management_1.1_spec-1.0.1.jar
    geronimo-jms_1.1_spec-1.1.1.jar
    slf4j-api-1.6.6.jar
    到项目中

    四、发送端

    package com.thomas.test;
    
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    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 TestMQSend {
    
    	
    	public void sendMQ(String news) {
    		
    		//连接工厂
    		ConnectionFactory connectionFactory;
    		
    		//连接
    		Connection connection = null;
    		
    		//收发线程
    		Session session;	
    		
    		//接收地址
    		Destination destination;
    		
    		//发方
    		MessageProducer messageProducer;
    		
    		//构造连接工厂实例化对象,利用Active实现
    		connectionFactory = new ActiveMQConnectionFactory(
    				ActiveMQConnection.DEFAULT_USER,
    				ActiveMQConnection.DEFAULT_PASSWORD,
    				"tcp://localhost:61616"
    				);
    		
    		try {
    			//创建连接 
    			connection = connectionFactory.createConnection();
    			//打开连接
    			connection.start();
    			//获得操作连接
    			session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
    			//设置接受地址
    			destination = session.createQueue("FirstQueue");
    			//获得发送方
    			messageProducer = session.createProducer(destination);
    			//设置是否持久化(根据实际设置)
    			messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    			
    			
    			
    			for(int i = 0;i < 5 ;i ++){
    				//设置消息内容
    				TextMessage message = session.createTextMessage("MQ信息 第"+ i + "轮次:" + news);
    				System.out.println("发送MQ信息 第"+ i + "轮次:" + news);
    				//发送消息
    				messageProducer.send(message);
    			}
    			
    			session.commit();
    			
    		} catch (JMSException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} finally{			
    			
    			if(connection != null){
    				
    				try {
    					connection.close();
    				} catch (JMSException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		}
    		
    		
    	}
    	
    	
    	
    	public static void main(String[] args) {
    		TestMQSend mq = new TestMQSend();
    		mq.sendMQ("测试MQ啦啦啦");
    	}
    }
    

     五、接收端

    package com.thomas.test;
    
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    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 TestMQRe {
    
    	
    	public void receiveMQ(){
    
    		
    		//连接工厂
    		ConnectionFactory connectionFactory;
    		
    		//连接
    		Connection connection = null;
    		
    		//收发线程
    		Session session;	
    		
    		//接收地址
    		Destination destination;
    		
    		//消息接受者
    		MessageConsumer consumer;
    		
    		connectionFactory = new ActiveMQConnectionFactory(
    				ActiveMQConnection.DEFAULT_USER,
    				ActiveMQConnection.DEFAULT_PASSWORD,
    				"tcp://localhost:61616"
    				);
    		
    		
    			
    			try {
    				//创建连接 
    				connection = connectionFactory.createConnection();
    				
    				//打开连接
    				connection.start();
    				
    				//获得操作连接
    				session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
    				
    				//设置接受地址
    				destination = session.createQueue("FirstQueue");
    				
    				//创建一个接受者
    				consumer = session.createConsumer(destination);
    				
    				//循环接受
    				while(true){
    					
    					TextMessage message = (TextMessage) consumer.receive(100000);
    					if(message != null){
    						
    						System.out.println("接受"+message.getText());
    					}else{
    						
    						break;
    					}
    				}
    				
    			} catch (JMSException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} finally{
    				
    				if(connection != null){
    					
    					try {
    						connection.close();
    					} catch (JMSException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
    			}
    
    		
    
    	}
    	
    	public static void main(String[] args) {
    		TestMQRe re = new TestMQRe();
    		re.receiveMQ();
    	}
    }
    

     六、结果

    发送端:
    发送MQ信息 第0轮次:测试MQ啦啦啦
    发送MQ信息 第1轮次:测试MQ啦啦啦
    发送MQ信息 第2轮次:测试MQ啦啦啦
    发送MQ信息 第3轮次:测试MQ啦啦啦
    发送MQ信息 第4轮次:测试MQ啦啦啦
    
    
    接收端:
    接受MQ信息 第0轮次:测试MQ啦啦啦
    接受MQ信息 第1轮次:测试MQ啦啦啦
    接受MQ信息 第2轮次:测试MQ啦啦啦
    接受MQ信息 第3轮次:测试MQ啦啦啦
    接受MQ信息 第4轮次:测试MQ啦啦啦
    
  • 相关阅读:
    1012 The Best Rank (25 分)(排序)
    1011. World Cup Betting (20)(查找元素)
    1009 Product of Polynomials (25 分)(模拟)
    1008 Elevator (20 分)(数学问题)
    1006 Sign In and Sign Out (25 分)(查找元素)
    1005 Spell It Right (20 分)(字符串处理)
    Kafka Connect 出现ERROR Failed to flush WorkerSourceTask{id=local-file-source-0}, timed out while wait
    flume、kafka、avro组成的消息系统
    Java23种设计模式总结【转载】
    Java编程 思维导图
  • 原文地址:https://www.cnblogs.com/ggwow/p/7641224.html
Copyright © 2011-2022 走看看