JMS 服务器ActiveMQ 安装与实例(持久化)

JMS 服务器ActiveMQ 安装与实例(持久化)

下载与安装

1、下载AMQ:http://download.csdn.net/detail/u014201191/8721759,最新版本是5.5.0;
2、解压apache-activemq-5.3.0-bin.zip文件到文件系统(比如D:\ActiveMQ-5.3.0);
3、执行bin/activemq.bat脚本即可启动AMQ:

 INFO | ActiveMQ 5.5.0 JMS Message Broker (localhost) is starting
 ......
 INFO | Listening for connections at: tcp://SHI-AP33382A:61616

 

出现上面时,表示成功,在浏览器中输入地址查看http://127.0.0.1:8161/admin/

测试实例

生产者:

/**
 * @author Administrator
 * @desctiption 结合一个例子,深入理解JMS的基本概念
 * 消息的消费者接收消息可以采用两种方式:
 * 1、consumer.receive() 或 consumer.receive(int timeout);
 * 2、注册一个MessageListener。
 * 采用第一种方式,消息的接收者会一直等待下去,直到有消息到达,或者超时。
 * 后一种方式会注册一个监听器,当有消息到达的时候,会回调它的onMessage()方法。
 */
package com.wl.jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

public class MessageSendAndReceive {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");  
	     
        Connection connection = factory.createConnection();  
        connection.start();  
        //创建消息的Destination 
        Queue queue = new ActiveMQQueue("testQueue");  
         
        final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
        //创建将要发送的消息
        Message message = session.createTextMessage("Hello JMS!");  
        //创建消息生产者发送消息 
        MessageProducer producer = session.createProducer(queue);  
        producer.send(message);  
     
        System.out.println("Send Message Completed!");  
        //创建消息的接收者 
//        MessageConsumer comsumer = session.createConsumer(queue); 
//        //消息的消费者接收消息的第一种方式:consumer.receive() 或 consumer.receive(int timeout);
//        //Message recvMessage = comsumer.receive();  
//        //System.out.println(((TextMessage)recvMessage).getText()); 
//        //消息的消费者接收消息的第二种方式:注册一个MessageListener
//        comsumer.setMessageListener(new MessageListener(){
//			public void onMessage(Message msg) {
//				// TODO Auto-generated method stub
//				TextMessage textMsg=(TextMessage)msg;
//				try {
//					System.out.println(textMsg.getText());
//				} catch (JMSException e) {
//					e.printStackTrace();
//				}
//			}
//        	
//        });
        //MessageSendAndReceive.getMessage();
	}
public static void getMessage() {
	try {
		Thread.sleep(1000*5);
		ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");  
	    Connection connection = factory.createConnection();  
	    connection.start();  
	    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
	    Queue queue = new ActiveMQQueue("testQueue"); 
	    MessageConsumer comsumer = session.createConsumer(queue); 
	    comsumer.setMessageListener(new MessageListener(){
			public void onMessage(Message msg) {
				// TODO Auto-generated method stub
				TextMessage textMsg=(TextMessage)msg;
				try {
					System.out.println(textMsg.getText());
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
	    	
	    });
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
}

 

消费者:

/**
 * @author Administrator
 * @desctiption 结合一个例子,深入理解JMS的基本概念
 * 消息的消费者接收消息可以采用两种方式:
 * 1、consumer.receive() 或 consumer.receive(int timeout);
 * 2、注册一个MessageListener。
 * 采用第一种方式,消息的接收者会一直等待下去,直到有消息到达,或者超时。
 * 后一种方式会注册一个监听器,当有消息到达的时候,会回调它的onMessage()方法。
 */
package com.wl.jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

public class MessageReceive {
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		
        MessageReceive.getMessage();
	}
public static void getMessage() {
	try {
		ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");  
	    Connection connection = factory.createConnection();  
	    connection.start();  
	    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
	    Queue queue = new ActiveMQQueue("testQueue"); 
	    MessageConsumer comsumer = session.createConsumer(queue); 
	    comsumer.setMessageListener(new MessageListener(){
			public void onMessage(Message msg) {
				// TODO Auto-generated method stub
				TextMessage textMsg=(TextMessage)msg;
				try {
					System.out.println(textMsg.getText());
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
	    });
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
}


数据持久化

ActiveMQ持久化方式:AMQ、KahaDB、JDBC、LevelDB 详见http://blog.csdn.net/xh16319/article/details/12144953

 

下面介绍JDBC-ORACLE的持久化方式

1、修改AMQ的默认配置文件 conf/activemq.xml 并将Oracle连接包放入lib目录下

2、添加Oracle数据源:默认情况下,AMQ使用KahaDB存储(我对KahaDB不了解),注释到KahaDB的配置方式,改为Oracle的:

        <!--
<persistenceAdapter>
    <kahaDB directory="${activemq.base}/data/kahadb"/>
</persistenceAdapter>
-->
<persistenceAdapter>
    <jdbcPersistenceAdapter dataSource="#Oracle-DS"/>
</persistenceAdapter>

配置Oracle数据源:在</broker>节点后面,增加Oracle数据源配置:

      <!-- Oracle DataSource -->
<bean id="Oracle-DS" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"/>
    <property name="username" value="lportal"/>
    <property name="password" value="lportal"/>
    <property name="poolPreparedStatements" value="true"/>
</bean>


3、重新启动AMQ,启动完成之后,我们发现,数据库多了3张数据表

sql> SHOW tables;
+----------------+
| Tables_in_misc |
+----------------+
| activemq_acks  |
| activemq_lock  |
| activemq_msgs  |
+----------------+

4、运行上面的程序,数据库的表会发生变化 activemq_msgs

 

帮助文档:http://obullxl.iteye.com/blog/1114827

ActiveMQ安装 : 1.下载:http://activemq.apache.org/download.html 最新Windows版本 2.配置jdk环境,解压ActiveMQ到本地 3.启动ActiveMQ服务器: 32位机 : 直接运行\bin\win32\activemq.bat 64位机 : 直接运行\bin\win64\activemq.bat 4.打开ActiveMQ消息管理后台系统 http://localhost:8161/admin/ 参考文章为 : HelloWorld : http://blog.csdn.net/baggio7095586/article/details/5708519 进阶 :http://haohaoxuexi.iteye.com/blog/1893038 helloworld阶段 : 一个发送消息的程序,一个接收消息的程序..实现最简单的JMS交流... 监听器阶段 : ⑴MessageListener,最简单的监听器..只是拿来监听接收信息的 ⑵SessionAwareMessageListener,有个session的参数..可以拿来对接收到的信息再做响应.. ⑶MessageListenerAdapter,将消息委托交给一个普通的java类来处理.. 转化器阶段 : 实现MessageConverter接口来转换JMS对象与java对象.. 其实在使用MessageListenerAdapter时,Spring默认为我们提供了SimpleMessageConverter转换器.. 事务(无代码): 在Spring整合JMS的应用中,如果我们要进行本地的事务管理的话非常简单,只需要在定义对应的消息监听容器时指定其sessionTransacted属性为true,如: <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queueDestination" /> <property name="messageListener" ref="consumerMessageListener" /> <property name="sessionTransacted" value="true"/> </bean> 该属性值默认为false,这样JMS在进行消息监听的时候就会进行事务控制,当在接收消息时监听器执行失败时JMS就会对接收到的消息进行回滚, 对于SessionAwareMessageListener在接收到消息后发送一个返回消息时也处于同一事务下,但是对于其他操作如数据库访问等将不属于该事务控制。 数据库跟JMS挂钩的事务先别理..
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值