package com.jiangchong.job;

import java.util.Date;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
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 javax.jms.Topic; import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue; public class JMS
{ public static void main(String[] args) throws Exception
{
consume();
product();
} public static void product() throws JMSException
{
// "tcp://localhost:61616"
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"vm:localhost:10000");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("testTopic");
MessageProducer producer = session.createProducer(topic);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
while (true)
{
TextMessage message = session.createTextMessage();
message.setText("message_" + new Date().toLocaleString());
producer.send(message);
try
{
Thread.sleep( * );
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
} public static void consume() throws JMSException
{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"vm:localhost");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("testTopic");
MessageConsumer consumer = session.createConsumer(topic);
consumer.setMessageListener(new MessageListener()
{
public void onMessage(Message message)
{
TextMessage tm = (TextMessage) message;
try
{
System.out.println("Received message: " + tm.getText());
}
catch (JMSException e)
{
e.printStackTrace();
}
}
});
} public static void p2p() throws JMSException
{
ConnectionFactory factory = new ActiveMQConnectionFactory(
"vm://localhost"); Connection connection = factory.createConnection();
connection.start(); Queue queue = new ActiveMQQueue("testQueue"); final Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
final Message message = session.createTextMessage(new Date()
.toLocaleString() + " Hello JMS! come from producer"); final MessageProducer producer = session.createProducer(queue);
new Thread(new Runnable()
{ public void run()
{
while (true)
{
try
{
producer.send(message);
}
catch (JMSException e)
{
e.printStackTrace();
}
try
{
Thread.sleep( * );
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }).start(); final MessageConsumer comsumer = session.createConsumer(queue);
new Thread(new Runnable()
{ public void run()
{
while (true)
{
Message recvMessage;
try
{
recvMessage = comsumer.receive();
System.out.println(((TextMessage) recvMessage)
.getText() + " rec");
}
catch (JMSException e)
{
e.printStackTrace();
}
try
{
Thread.sleep( * 1000L);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}).start();
} }

jms的俩种模式的更多相关文章

  1. 消息队列:快速上手ActiveMQ消息队列的JMS方式使用(两种模式:Topic和Queue的消息推送和订阅)

    1.实现功能 希望使用一套API,实现两种模式下的消息发送和接收功能,方便业务程序调用 1.发送Topic 2.发送Queue 3.接收Topic 4.接收Queue 2.接口设计 根据功能设计公共调 ...

  2. JMS是一种应用于异步消息传递的标准API

    JMS是一种应用于异步消息传递的标准API,作为Java平台的一部分,JMS可以允许不同应用.不同模块之间实现可靠.异步数据通信.一些概念 JMS provider    An implementat ...

  3. javascript 创建对象的7种模式

    使用字面量方式创建一个 student 对象: var student = function (){ name : "redjoy", age : 21, sex: women, ...

  4. javascript面向对象系列第二篇——创建对象的5种模式

    × 目录 [1]字面量 [2]工厂模式 [3]构造函数[4]原型模式[5]组合模式 前面的话 如何创建对象,或者说如何更优雅的创建对象,一直是一个津津乐道的话题.本文将从最简单的创建对象的方式入手,逐 ...

  5. javascript创建对象的几种模式

    在js中有几种模式可以创建对象,通过对象操作所包含的属性与方法. 一般来说,构造函数名称的第一个字母为大写字母,非构造函数名称的第一个字母为小写字母,当然,构造函数与一般函数唯一的区别只是调用的方式不 ...

  6. SQL Server 2012复制教程以及复制的几种模式

    简介 SQL Server中的复制(Replication)是SQL Server高可用性的核心功能之一,在我看来,复制指的并不仅仅是一项技术,而是一些列技术的集合,包括从存储转发数据到同步数据到维护 ...

  7. git push :推送本地更改到远程仓库的三种模式

    摘要:由于在git push过程中,no-fast-forward 的push会被拒绝,如何解决git push失败的问题?这里面有三种方法,分别会形成merge形式的提交历史,线性形式的提交历史,覆 ...

  8. App开发三种模式

    APP开发三种模式 现在App开发的模式包含以下三种: Native App 原生开发AppWeb App 网页AppHybrid App 混合原生和Web技术开发的App 详细介绍: http:// ...

  9. [转]VMware Workstation网络连接的三种模式

    经常要使用VMWare Workstation来在本地测试不同的操作系统,以前也搞不清楚网络连接三种模式,最近看了几篇文章才算明白.现总结如下: 1. VMware Workstation的虚拟网络组 ...

随机推荐

  1. 对tomcat中使用反射加载类的理解

    public void init() throws Exception { initClassLoaders(); //加载一下jar包和类 Thread.currentThread().setCon ...

  2. LeetCode-Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  3. [mobi文件格式详细介绍一]_[资料篇]

    Amazon 收购 Mobipocket 于 2005年3月: Kindle 第一版于2007发布,mobi成为其系统支持的电子格式之一,mobi是由kindle带动发展起来的; 有人问Amazon为 ...

  4. MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)【转】

    MVC 默认 Request 方式为Get. actionpublic JsonResult GetPersonInfo(){var person = new{Name = "张三" ...

  5. winform上控件太多,绘制时会逐个出现,通常说双缓冲能解决但实际不能解决的问题的解决方法。

    protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle ...

  6. api接口验证shal()

    就安全来说,所有客户端和服务器端的通信内容应该都要通过加密通道(HTTPS)传输,明文的HTTP通道将会是man-in-the- middle及其各种变种攻击的温床.所谓man-in-the-midd ...

  7. MyEclipse、Eclipse复制web项目

    假设现在已经有一个Java Web项目 ProjectA,现在想做另外一个项目ProjectB,它里面绝大部分功能和结构都可以复用ProjectA中的: 我们就可以通过复制项目的方法来做,具体步骤如下 ...

  8. 小表驱动大表, 兼论exists和in

    给出两个表,A和B,A和B表的数据量, 当A小于B时,用exists select * from A where exists (select * from B where A.id=B.id) ex ...

  9. Format specifies type 'int' but the argument has type 'struct node *'

    /Users/Rubert/IOS/iworkspace/LineList/LineList/main.c::: Format specifies type 'int' but the argumen ...

  10. STM32串口

    状态寄存器USART_SR的复位值为0x00C0H:即 第七位发送寄存器空TXE=1和第六位发送完成TC=1寄存器为空, TC=1表明发送已完成.此时只要使能了发送空和发送完成中断就可进入中断ISR( ...