package com.jms;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.clapper.util.logging.Logger; import com.pzoom.dsa.common.util.Log;
import com.pzoom.dsa.nerd.mysql.DBQueryHelper; public class Jms
{
static ConnectionFactory connectionFactory;
static Connection connection = null;
static Session session;
static Map<String, MessageProducer> sendQueues = new ConcurrentHashMap<String, MessageProducer>(); static Map<String, MessageConsumer> getQueues = new ConcurrentHashMap<String, MessageConsumer>(); static Log log=Log.getLogger(DBQueryHelper.class); static {
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://10.100.100.100:61616?wireFormat.maxInactivityDuration=0");
try
{
connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(Boolean.FALSE.booleanValue(),
1);
}
catch (Exception e) {
e.printStackTrace();
}
} static MessageProducer getMessageProducer(String name) {
if (sendQueues.containsKey(name))
return ((MessageProducer)sendQueues.get(name));
try
{
Destination destination = session.createQueue(name);
MessageProducer producer = session.createProducer(destination);
sendQueues.put(name, producer);
return producer;
} catch (JMSException e) {
e.printStackTrace();
} return ((MessageProducer)sendQueues.get(name));
} static MessageConsumer getMessageConsumer(String name) {
if (getQueues.containsKey(name))
return ((MessageConsumer)getQueues.get(name));
try
{
Destination destination = session.createQueue(name);
MessageConsumer consumer = session.createConsumer(destination);
getQueues.put(name, consumer);
return consumer;
} catch (JMSException e) {
e.printStackTrace();
} return ((MessageConsumer)getQueues.get(name));
} public static void sendMessage(String queue, String text) {
try {
TextMessage message = session.createTextMessage(text);
getMessageProducer(queue).send(message);
// log.info("sendMessage " + queue + "\t\t" + text);
}
catch (JMSException e) {
e.printStackTrace();
}
} public static String getMessage(String queue)
{
try {
TextMessage message = (TextMessage)getMessageConsumer(queue).receive(10000L);
if (message != null)
return message.getText();
} catch (JMSException e) {
e.printStackTrace();
}
return null;
} public static void close() {
try {
session.close();
} catch (JMSException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}

activeMQ公布订阅模式中中经常使用工具类的更多相关文章

  1. Redis源代码分析(三十)--- pubsub公布订阅模式

    今天学习了Redis中比較高大上的名词,"公布订阅模式".公布订阅模式这个词在我最開始接触听说的时候是在JMS(Java Message Service)java消息服务中听说的. ...

  2. 获取Spring容器中Bean实例的工具类(Java泛型方法实现)

    在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...

  3. commons-collections包中的常用的工具类

    commons-collections包中的常用的工具类 <dependency> <groupId>commons-collections</groupId> & ...

  4. Hutool中那些常用的工具类和方法

    Hutool中那些常用的工具类和方法 Hutool是一个Java工具包,它帮助我们简化每一行代码,避免重复造轮子.如果你有需要用到某些工具方法的时候,不妨在Hutool里面找找,可能就有.本文将对Hu ...

  5. Shuttle ESB(四)——公布订阅模式实例介绍(1)

    前面,我已经集中用了三篇文章来讲Shuttle ESB的入门实例与宏观概念. Shuttle ESB一共同拥有两种发送消息的模式:请求/对应模式与Pub/Sub模式. 关于这两种模式的区分.请看以下文 ...

  6. ActiveMQ发布订阅模式

    ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...

  7. ActiveMQ发布订阅模式(转)

    ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...

  8. ActiveMQ发布订阅模式 转发 https://www.cnblogs.com/madyina/p/4127144.html

    ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...

  9. C# 中那些常用的工具类(Utility Class)(二)

    今天按照这一年来经常用到的那些静态的工具类再来做一次总结,这些小的工具来可以作为自己学习的很好的例子,通过总结这些东西,能够很大程度上梳理自己的知识体系,当然这个是经常用到的,接下来就一个个去分析这些 ...

随机推荐

  1. 题解报告:hdu 1232 畅通工程(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了 ...

  2. CSS发布时间

    * 1996年W3C正式推出了CSS1.* 1998年W3C正式推出了CSS2.* CSS2.1是W3C现在正在推荐使用的.* CSS3现在还处于开发中.

  3. 使用UAParser在C#MVC项目中如何判断用户是在用什么设备进行访问(手机,平板还是普通的电脑)

    现在我们开发的很多web应用都要支持手机等移动设备.为了让手机用户能有更加好的用户体验,我们经常为手机设备专门准备一套前端的页面.这样当用户使用普通电脑来访问的时候,我们的应用就向用户展示普通电脑的页 ...

  4. html5——动画案例(大海)

    太阳的发散效果主要是利用transform: scale(1.3),将物体变大 <!DOCTYPE html> <html lang="en"> <h ...

  5. 在CentOS下搭建Android 开发环境

    在CentOS下搭建Android 开发环境 目录 1.环境搭建 1.1.JDK安装 1.2.Eclipse安装 1.3.ADT安装 1.4.Android SDK安装 1.5.Android NDK ...

  6. Lazarus Reading XML- with TXMLDocument and TXPathVariable

    也就是使用XPath的方式,具体语法规则查看http://www.w3school.com.cn/xpath/xpath_syntax.asp,说明得相当详细.这里列举例子是说明在Lazarus/FP ...

  7. 【sqli-labs】 less46 GET -Error based -Numeric -Order By Clause(GET型基于错误的数字型Order By从句注入)

    http://192.168.136.128/sqli-labs-master/Less-46/?sort=1 sort=4时出现报错 说明参数是添加在order by 之后 错误信息没有屏蔽,直接使 ...

  8. (转)Struts2访问Servlet的API及......

    http://blog.csdn.net/yerenyuan_pku/article/details/67315598 Struts2访问Servlet的API 前面已经对Struts2的流程已经执行 ...

  9. accmcolor

    accmcolor c; c.setcolorindex(1); playertablerecord.setcolor(c);

  10. CAD处理键盘按钮被释放(com接口VB语言)

    主要用到函数说明: MxDrawXCustomEvent::KeyUp 键盘按钮被释放,详细说明如下: 参数 说明 iVk 是按钮码,如F8,的值为#define VK_F8 0x77 返回0消息继续 ...