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. 从源码看ASP.NET框架(一)-打造页面控件树

    测试实例如下: 前台代码MyFirstWeb.aspx(没有服务器控件,即没有runat) CodeBehind="MyFirstWeb.aspx.cs":表示代码后置类文件 In ...

  2. [ SHOI 2014 ] 概率充电器

    \(\\\) \(Description\) 一个含\(N\)个元器件的树形结构充电器,第\(i\)个元器件有\(P_i\)的概率直接从外部被充电,连接\(i,j\)的边有\(P_{i,j}\)的概率 ...

  3. SQL基本操作——通配符

    SQL 通配符:在搜索数据库中的数据时,SQL 通配符可以替代一个或多个字符.SQL 通配符必须与 LIKE 运算符一起使用.在 SQL 中,可使用以下通配符: 通配符 描述 % 替代一个或多个字符 ...

  4. SQL基本操作——函数

    函数的类型:在 SQL 中,基本的函数类型和种类有若干种.函数的基本类型是:Aggregate 函数.Scalar 函数. Aggregate 函数:操作面向一系列的值,并返回一个单一的值,下面是SQ ...

  5. 显示log里的ansi codecs颜色字符

    方法: vim AnsiEsc插件 http://www.vim.org/scripts/script.php?script_id=302 less -r cat和tail命令都可以正常显示,而且ta ...

  6. POJ 3070 - 快速矩阵幂求斐波纳契数列

    这题并不复杂. 设$A=\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}$ 由题中公式: $\begin{pmatrix}f(n+1) & ...

  7. Mysql 之show status数据详解

    状态名 作用域 详细解释 Aborted_clients Global 由于客户端没有正确关闭连接导致客户端终止而中断的连接数 Aborted_connects Global 试图连接到MySQL服务 ...

  8. PHP 之递归遍历目录与删除

    /** * @Description: 递归查询目录文件 * @Author: Yang * @param $path * @param int $level * @return array */ f ...

  9. JavaFX桌面应用开发-Button(按钮)与事件

    1:Button样式的操作原始代码: package application; import javafx.application.Application;import javafx.scene.Gr ...

  10. python tkinter模块 创建窗口V1.2

    先上图 代码如下 #-*-coding:utf-8-*- import os from tkinter import * root=Tk() root.title('执行窗口') "&quo ...