activemq 连接mysql_ActiveMQ Spring 整合持久化到数据库的实现

本文主要目的实现activemq和spring将消息写入数据库的方法:

activemq.xml的内容如下:

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

activemq-jdbc.xml

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

消息生产者:

package easyway.activemq.app.demo3;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import javax.jms.Connection;

import javax.jms.Destination;

import javax.jms.JMSException;

import javax.jms.MessageProducer;

import javax.jms.Session;

import javax.jms.StreamMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

* 消息的创建者

* @author longgangbai

*

*/

public class StreamMsgProducer {

public static void main(String[] args) {

ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("activemq-jdbc.xml");

ActiveMQConnectionFactory activeMqfactory=(ActiveMQConnectionFactory)ctx.getBean("connectionFactory");

Connection conn = null;

try {

conn = activeMqfactory.createConnection();

conn.start();

Session session = conn.createSession(false,

Session.AUTO_ACKNOWLEDGE);

Destination queue = session.createQueue("streamMsg");

MessageProducer producer = session.createProducer(queue);

File file=new File("C:\\send.txt");

InputStream in = new FileInputStream(file);

byte[] buffer = new byte[2048];

int c = -1;

while ((c = in.read(buffer)) > 0) {

StreamMessage smsg = session.createStreamMessage();

smsg.writeBytes(buffer, 0, c);

producer.send(smsg);

System.out.println("send: " + c);

}

in.close();

} catch (JMSException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (conn != null) {

try {

conn.close();

} catch (JMSException e) {

e.printStackTrace();

}

}

}

}

}

消息消费者:

package easyway.activemq.app.demo3;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import javax.jms.Connection;

import javax.jms.Destination;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.MessageConsumer;

import javax.jms.Session;

import javax.jms.StreamMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

import org.apache.activemq.xbean.BrokerFactoryBean;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

* 消息的消费者

* @author longgangbai

*

*/

public class StreamMsgConsumer {

public void receive() {

ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("activemq-jdbc.xml");

ActiveMQConnectionFactory activeMqfactory=(ActiveMQConnectionFactory)ctx.getBean("connectionFactory");

Connection conn = null;

try {

conn = activeMqfactory.createConnection();

conn.start();

Session session = conn.createSession(false,

Session.AUTO_ACKNOWLEDGE);

Destination queue = session.createQueue("streamMsg");

MessageConsumer consumer = session.createConsumer(queue);

OutputStream out = new FileOutputStream("c:\\receive.txt");

byte[] buffer = new byte[2048];

while (true) {

Message msg = consumer.receive(5000);

if (msg == null) {

break;

}

if (msg instanceof StreamMessage) {

StreamMessage smsg = (StreamMessage) msg;

int c = smsg.readBytes(buffer);

out.write(buffer, 0, c);

System.out.println("Receive: " + c);

}

}

out.close();

} catch (JMSException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (conn != null) {

try {

conn.close();

} catch (JMSException e) {

e.printStackTrace();

}

}

}

}

public static void main(String[] args) {

new StreamMsgConsumer().receive();

}

}

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-06-03 23:24

浏览 4505

评论

1 楼

phane

2012-03-06

38687d1a1ad71d37c86f287056834d1a.gif

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值