activeMQ 试用
来源:互联网 发布:adobe设计软件 编辑:程序博客网 时间:2024/06/08 14:59
百度搜索一些资料。尝试写个DEMO
不适用任何框架。
先登录MQ地址添加消息队列。
队列名称 test.queuq
编辑mq.properties
mq.queue = test.queue
mq.url = tcp://xxx.xxx.xxx.xxx:61616
消息发送者代码:
import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.DeliveryMode;import javax.jms.Destination;import javax.jms.MessageProducer;import javax.jms.Session;import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;
public class TestSender { private static final int SEND_NUMBER = 1;
public static void main(String[] args) { // ConnectionFactory :连接工厂,JMS 用它创建连接 ConnectionFactory connectionFactory; // Connection :JMS 客户端到JMS Provider 的连接 Connection connection = null; // Session: 一个发送或接收消息的线程 Session session; // Destination :消息的目的地;消息发送给谁. Destination destination; // MessageProducer:消息发送者 MessageProducer producer; // TextMessage message; // 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://172.16.40.80:61616"); try { // 构造从工厂得到连接对象 connection = connectionFactory.createConnection(); // 启动 connection.start(); // 获取操作连接 session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 destination = session.createQueue("test.queue"); // 得到消息生成者【发送者】 producer = session.createProducer(destination); // 设置不持久化,此处学习,实际根据项目决定 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // 构造消息,此处写死,项目就是参数,或者方法获取 sendMessage(session, producer); session.commit(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != connection) connection.close(); } catch (Throwable ignore) { } } }
public static void sendMessage(Session session, MessageProducer producer) throws Exception { String msg = "[{\"modelId\": \"00000001\",\"model\": \"熊猫\"},{\"modelId\":\"00000006\" ,\"model\":\"土豆\"}]"; for (int i = 1; i <= SEND_NUMBER; i++) { TextMessage message = session .createTextMessage(msg); // 发送消息到目的地方 System.out.println("[{\"modelId\": \"00000001\",\"model\": \"熊猫\"},{\"modelId\":\"00000006\" ,\"model\":\"土豆\"}]"); producer.send(message); } }
消息接收者,这里使用messageListener
import javax.jms.Connection;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageConsumer;import javax.jms.MessageListener;import javax.jms.Session;import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import com.mig.encrypt.dao.impl.SyncInDB;import com.mig.encrypt.pojo.BaseEntity;import com.mig.encrypt.util.config.MqConfig;
public class MqUtil<T extends BaseEntity> implements MessageListener {
static Logger logger = LoggerFactory.getLogger(MqUtil.class);
private String user = ActiveMQConnection.DEFAULT_USER;
private String password = ActiveMQConnection.DEFAULT_PASSWORD;
private String url = MqConfig.getInstance().getConfigItem("mq.url");
private String subject = MqConfig.getInstance().getConfigItem("mq.queue");
private Destination destination = null;
private Connection connection = null;
private Session session = null;
private MessageConsumer consumer = null;
private Class<?> z;
//构造,传入POJO
public MqUtil(Class<?> z) { this.z = z; }
private void initialize() throws JMSException, Exception {
logger.debug("MqUtil - [initialize]: initialize is begin");
// 连接工厂是用户创建连接的对象,这里使用的是ActiveMQ的ActiveMQConnectionFactory根据url,username和password创建连接工厂。 ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( user, password, url); // 连接工厂创建一个jms connection connection = connectionFactory.createConnection(); // 是生产和消费的一个单线程上下文。会话用于创建消息的生产者,消费者和消息。会话提供了一个事务性的上下文。 session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 不支持事务 // 目的地是客户用来指定他生产消息的目标还有他消费消息的来源的对象,两种消息传递方式:点对点和发布/订阅 destination = session.createQueue(subject); // 会话创建消息的生产者将消息发送到目的地 consumer = session.createConsumer(destination);
logger.debug("MqUtil - [initialize]: initialize is end"); }
public void close() throws JMSException { logger.debug("MqUtil - [close]: close is begin"); if (consumer != null) { consumer.close(); } if (session != null) { session.close(); } if (connection != null) { connection.close(); } logger.debug("MqUtil - [close]: close is end"); }
// 消费消息 public void handleMessage() throws JMSException, Exception { logger.debug("MqUtil - [handleMessage]: handleMessage is begin"); initialize(); connection.start();
// 开始监听 consumer.setMessageListener(this);
logger.debug("MqUtil - [handleMessage]: handleMessage is end"); }
@Override public void onMessage(Message message) { logger.debug("MqUtil - [onMessage]: onMessage is begin"); try { if (message instanceof TextMessage) { TextMessage txtMsg = (TextMessage) message; String msg = txtMsg.getText(); logger.debug("MqUtil - [onMessage]: message is " + msg); new SyncInDB().syncContent(msg, z); } else { } } catch (JMSException e) { logger.error("MqUtil - [onMessage]: JMSException catched : " + e.getMessage()); } logger.debug("MqUtil - [onMessage]: onMessage is end"); }
}
服务器启动时,开始监听消息<listener> <listener-class> com.test.listener.ServerListener </listener-class> </listener>
serverListener代码,启动时开始监听,服务结束时关闭连接。import javax.jms.JMSException;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import com.mig.encrypt.pojo.MigModel;import com.mig.encrypt.util.MqUtil;
public class ServerListener implements ServletContextListener {
Logger logger = LoggerFactory.getLogger(ServerListener.class);
private MqUtil<MigModel> mqUtil = new MqUtil<MigModel>(MigModel.class);
@Override public void contextDestroyed(ServletContextEvent arg0) { logger.debug("ServerListener - [contextDestroyed]: contextDestroyed is begin,server is down,listener is down"); try { mqUtil.close(); } catch (JMSException e) { logger.error("ServerListener - [contextDestroyed]: JMSException catched : " + e.getMessage()); } logger.debug("ServerListener - [contextDestroyed]: contextDestroyed is end"); }
@Override public void contextInitialized(ServletContextEvent arg0) { logger.debug("ServerListener - [contextInitialized]: contextInitialized is begin,server is begin,listener is begin"); try { mqUtil.consumeMessage(); } catch (JMSException e) { logger.error("ServerListener - [contextInitialized]: JMSException catched : " + e.getMessage()); } catch (Exception e) { logger.error("ServerListener - [contextInitialized]: Exception catched : " + e.getMessage()); } logger.debug("ServerListener - [contextInitialized]: contextInitialized is end"); }
}
- activeMQ 试用
- 试用
- 试用
- 试用
- 试用
- 试用
- 试用
- 试用
- 试用
- ActiveMQ
- ACtiveMQ
- ACtiveMQ
- activeMQ
- ActiveMQ
- activeMQ
- activeMQ
- activemq
- ActiveMQ
- ubuntu下svn使用指南
- ORACLE 笔记
- jprofiler 监控远程linux tomcat
- maven + tomcat7 redeploy
- 总结 ubuntu 11.10 安装配置 JDK7 +TOMCAT7+MYECLIPSE9.1+MYSQL 5.1
- activeMQ 试用
- java设置断点调试技巧
- SELinux策略语言--类型强制(编写TE规则)
- 向剪贴板中放入数据
- SQL SERVER中将某字段默认值设置为当前登录名
- iOS 开发 百度地图 在模拟器上只显示网格!
- log4j配置详解
- 【Lucene3.6.2入门系列】第14节_SolrJ操作索引和搜索文档以及整合中文分词
- 分析方法论探讨之设计意图