- 浏览: 23505 次
- 性别:
- 来自: 厦门
最新评论
-
hpyhgz:
例子不错。。
JMS -
wst0350:
感谢
JMS -
linliangyi2007:
好帖,代码清晰明了。超级喜欢lz这样的教学贴,建议斑竹加精哦! ...
JMS -
liwei:
谢谢lz!正在学习中
JMS -
pig345:
楼上正解,svn不是很简单的东西,看过文档再说也不迟,呵呵。
版本控制,Subversion
1.PTP模型应用
package com.xmddl.jms.demo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Hashtable; import javax.jms.BytesMessage; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.ObjectMessage; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.jms.Session; import javax.jms.StreamMessage; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class QueueSend { // Defines the JNDI context factory. public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory"; // Defines the JNDI provider url. public final static String PROVIDER_URL = "t3://localhost:7001"; // Defines the JMS connection factory for the queue. public final static String JMS_FACTORY = "SendJMSFactory"; // Defines the queue. public final static String QUEUE = "SendJMSQueue"; private QueueConnectionFactory qconFactory; private QueueConnection qcon; private QueueSession qsession; private QueueSender qsender; private Queue queue; private TextMessage msg; private StreamMessage sm; private BytesMessage bm; private MapMessage mm; private ObjectMessage om; /** * Creates all the necessary objects for sending messages to a JMS queue. * * @param ctx * JNDI initial context * @param queueName * name of queue * @exception NamingException * if operation cannot be performed * @exception JMSException * if JMS fails to initialize due to internal error */ public void init(Context ctx, String queueName) throws NamingException, JMSException { qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY); qcon = qconFactory.createQueueConnection(); qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queue = (Queue) ctx.lookup(queueName); qsender = qsession.createSender(queue); msg = qsession.createTextMessage(); sm = qsession.createStreamMessage(); bm = qsession.createBytesMessage(); mm = qsession.createMapMessage(); om = qsession.createObjectMessage(); qcon.start(); } /** * Sends a message to a JMS queue. * * @param message * message to be sent * @exception JMSException * if JMS fails to send message due to internal error */ public void send(String message) throws JMSException { // set TextMessage msg.setText(message); // set StreamMessage sm.writeString("xmddl369"); sm.writeDouble(23.33); // set BytesMessage String name = "xmddl369"; byte[] block = name.getBytes(); bm.writeBytes(block); // set MapMessage mm.setString("name", "xmddl369"); // set ObjectMessage UserInfo ui = new UserInfo(); ui.setName("xmddl369"); ui.setAddress("厦门"); ui.setAge(100); om.setObject(ui); // qsender.send(msg); // qsender.send(sm); // qsender.send(bm); // qsender.send(mm); qsender.send(om); } /** * Closes JMS objects. * * @exception JMSException * if JMS fails to close objects due to internal error */ public void close() throws JMSException { qsender.close(); qsession.close(); qcon.close(); } public static void main(String[] args) throws Exception { InitialContext ic = getInitialContext(); QueueSend qs = new QueueSend(); qs.init(ic, QUEUE); readAndSend(qs); qs.close(); } private static void readAndSend(QueueSend qs) throws IOException, JMSException { BufferedReader msgStream = new BufferedReader(new InputStreamReader( System.in)); String line = null; boolean quitNow = false; do { System.out.print("Enter message (\"quit\" to quit): "); line = msgStream.readLine(); if (line != null && line.trim().length() != 0) { qs.send(line); System.out.println("JMS Message Sent: " + line + "\n"); quitNow = line.equalsIgnoreCase("quit"); } } while (!quitNow); } private static InitialContext getInitialContext() throws NamingException { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY); env.put(Context.PROVIDER_URL, PROVIDER_URL); return new InitialContext(env); } }
package com.xmddl.jms.demo; import java.util.Hashtable; import javax.jms.BytesMessage; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueReceiver; import javax.jms.QueueSession; import javax.jms.Session; import javax.jms.StreamMessage; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; /** * * @author Administrator * <pre> * 修改版本: 修改人: 修改日期: 修改内容: * </pre> */ public class QueueReceive implements MessageListener { // Defines the JNDI context factory. public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory"; // Defines the JNDI provider url. public final static String PROVIDER_URL = "t3://localhost:7001"; // Defines the JMS connection factory for the queue. public final static String JMS_FACTORY = "SendJMSFactory"; // Defines the queue. public final static String QUEUE = "SendJMSQueue"; private QueueConnectionFactory qconFactory; private QueueConnection qcon; private QueueSession qsession; private QueueReceiver qreceiver; private Queue queue; private boolean quit = false; /** * Message listener interface. * * @param msg * message */ public void onMessage(Message msg) { try { String msgText = ""; double d = 0; if (msg instanceof TextMessage) { msgText = ((TextMessage) msg).getText(); } else if (msg instanceof StreamMessage) { msgText = ((StreamMessage) msg).readString(); d = ((StreamMessage) msg).readDouble(); } else if (msg instanceof BytesMessage) { byte[] block = new byte[1024]; ((BytesMessage) msg).readBytes(block); msgText = String.valueOf(block); } else if (msg instanceof MapMessage) { msgText = ((MapMessage) msg).getString("name"); } else if (msg instanceof ObjectMessage) { UserInfo ui = (UserInfo) ((ObjectMessage) msg).getObject(); msgText = ui.getName(); d = ui.getAge(); } System.out.println("Message Received: " + msgText + "\t" + d); if (msgText.equalsIgnoreCase("quit")) { synchronized (this) { quit = true; this.notifyAll(); // Notify main thread to quit } } } catch (JMSException jmse) { jmse.printStackTrace(); } } /** * Creates all the necessary objects for receiving messages from a JMS * queue. * * @param ctx * JNDI initial context * @param queueName * name of queue * @exception NamingException * if operation cannot be performed * @exception JMSException * if JMS fails to initialize due to internal error */ public void init(Context ctx, String queueName) throws NamingException, JMSException { qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY); qcon = qconFactory.createQueueConnection(); qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queue = (Queue) ctx.lookup(queueName); qreceiver = qsession.createReceiver(queue); qreceiver.setMessageListener(this); qcon.start(); } /** * Closes JMS objects. * * @exception JMSException * if JMS fails to close objects due to internal error */ public void close() throws JMSException { qreceiver.close(); qsession.close(); qcon.close(); } /** * main() method. * * @param args * WebLogic Server URL * @exception Exception * if execution fails */ public static void main(String[] args) throws Exception { InitialContext ic = getInitialContext(); QueueReceive qr = new QueueReceive(); qr.init(ic, QUEUE); System.out .println("JMS Ready To Receive Messages (To quit, send a \"quit\" message)."); // Wait until a "quit" message has been received. synchronized (qr) { while (!qr.quit) { try { qr.wait(); } catch (InterruptedException ie) { ie.printStackTrace(); } } } qr.close(); } private static InitialContext getInitialContext() throws NamingException { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY); env.put(Context.PROVIDER_URL, PROVIDER_URL); return new InitialContext(env); } public String getEncoding() throws Exception { return "Hello World!"; } }
package com.xmddl.jms.demo; import java.io.Serializable; public class UserInfo implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private String name; private String address; private double age; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public double getAge() { return age; } public void setAge(double age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
2.PUB/SUB模型应用
package com.xmddl.jms.demo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Hashtable; import javax.jms.BytesMessage; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.ObjectMessage; import javax.jms.Session; import javax.jms.StreamMessage; import javax.jms.TextMessage; import javax.jms.Topic; import javax.jms.TopicConnection; import javax.jms.TopicConnectionFactory; import javax.jms.TopicPublisher; import javax.jms.TopicSession; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class TopicSend { // Defines the JNDI context factory. public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory"; // Defines the JNDI provider url. public final static String PROVIDER_URL = "t3://localhost:7001"; // Defines the JMS connection factory for the queue. public final static String JMS_FACTORY = "SendJMSFactory"; // Defines the queue. public final static String TOPIC= "SendJMSTopic"; private TopicConnectionFactory qconFactory; private TopicConnection qcon; private TopicSession qsession; private TopicPublisher publisher; private Topic topic; private TextMessage msg; private StreamMessage sm; private BytesMessage bm; private MapMessage mm; private ObjectMessage om; public void init(Context ctx, String queueName) throws NamingException, JMSException { qconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY); qcon = qconFactory.createTopicConnection(); // qsession = qcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); qsession = qcon.createTopicSession(true, Session.AUTO_ACKNOWLEDGE); topic = (Topic) ctx.lookup(queueName); publisher = qsession.createPublisher(topic); msg = qsession.createTextMessage(); sm=qsession.createStreamMessage(); bm=qsession.createBytesMessage(); mm=qsession.createMapMessage(); om=qsession.createObjectMessage(); qcon.start(); } /** * Sends a message to a JMS queue. * * @param message * message to be sent * @exception JMSException * if JMS fails to send message due to internal error */ public void send(String message) throws JMSException { //set TextMessage msg.setText(message); //set StreamMessage sm.writeString("xmddl369"); sm.writeDouble(23.33); //set BytesMessage String name="xmddl369"; byte[] block=name.getBytes(); bm.writeBytes(block); //set MapMessage mm.setString("name", "xmddl369"); //set ObjectMessage UserInfo ui=new UserInfo(); ui.setName("xmddl369"); ui.setAddress("厦门"); ui.setAge(100); om.setObject(ui); // qsender.send(msg); // qsender.send(sm); // qsender.send(bm); // qsender.send(mm); publisher.publish(om); // qsession.commit(); } /** * Closes JMS objects. * * @exception JMSException * if JMS fails to close objects due to internal error */ public void close() throws JMSException { publisher.close(); qsession.close(); qcon.close(); } public static void main(String[] args) throws Exception { InitialContext ic = getInitialContext(); TopicSend qs = new TopicSend(); qs.init(ic, TOPIC); readAndSend(qs); qs.close(); } private static void readAndSend(TopicSend qs) throws IOException, JMSException { BufferedReader msgStream = new BufferedReader(new InputStreamReader( System.in)); String line = null; boolean quitNow = false; do { System.out.print("Enter message (\"quit\" to quit): "); line = msgStream.readLine(); if (line != null && line.trim().length() != 0) { qs.send(line); System.out.println("JMS Message Sent: " + line + "\n"); quitNow = line.equalsIgnoreCase("quit"); } } while (!quitNow); } private static InitialContext getInitialContext() throws NamingException { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY); env.put(Context.PROVIDER_URL, PROVIDER_URL); return new InitialContext(env); } }
package com.xmddl.jms.demo; import java.util.Hashtable; import javax.jms.BytesMessage; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; import javax.jms.Session; import javax.jms.StreamMessage; import javax.jms.TextMessage; import javax.jms.Topic; import javax.jms.TopicConnection; import javax.jms.TopicConnectionFactory; import javax.jms.TopicSession; import javax.jms.TopicSubscriber; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class TopicReceive implements MessageListener{ // Defines the JNDI context factory. public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory"; // Defines the JNDI provider url. public final static String PROVIDER_URL = "t3://localhost:7001"; // Defines the JMS connection factory for the queue. public final static String JMS_FACTORY = "SendJMSFactory"; // Defines the queue. public final static String TOPIC= "SendJMSTopic"; private TopicConnectionFactory qconFactory; private TopicConnection qcon; private TopicSession qsession; private TopicSubscriber subscriber; private Topic topic; private TextMessage msg; private boolean quit = false; public void onMessage(Message msg) { try { String msgText = ""; double d = 0; if (msg instanceof TextMessage) { msgText = ((TextMessage) msg).getText(); } else if (msg instanceof StreamMessage) { msgText = ((StreamMessage) msg).readString(); d = ((StreamMessage) msg).readDouble(); } else if (msg instanceof BytesMessage) { byte[] block = new byte[1024]; ((BytesMessage) msg).readBytes(block); msgText = String.valueOf(block); } else if (msg instanceof MapMessage) { msgText = ((MapMessage) msg).getString("name"); } else if (msg instanceof ObjectMessage) { UserInfo ui = (UserInfo) ((ObjectMessage) msg).getObject(); msgText = ui.getName(); d = ui.getAge(); } System.out.println("Message Received: " + msgText + "\t" + d); if (msgText.equalsIgnoreCase("quit")) { synchronized (this) { quit = true; this.notifyAll(); // Notify main thread to quit } } } catch (JMSException jmse) { jmse.printStackTrace(); } } /** * Creates all the necessary objects for receiving messages from a JMS * queue. * * @param ctx * JNDI initial context * @param queueName * name of queue * @exception NamingException * if operation cannot be performed * @exception JMSException * if JMS fails to initialize due to internal error */ public void init(Context ctx, String queueName) throws NamingException, JMSException { qconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY); qcon = qconFactory.createTopicConnection(); qsession = qcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); topic = (Topic) ctx.lookup(queueName); subscriber = qsession.createSubscriber(topic); subscriber.setMessageListener(this); qcon.start(); } /** * Closes JMS objects. * * @exception JMSException * if JMS fails to close objects due to internal error */ public void close() throws JMSException { subscriber.close(); qsession.close(); qcon.close(); } /** * main() method. * * @param args * WebLogic Server URL * @exception Exception * if execution fails */ public static void main(String[] args) throws Exception { InitialContext ic = getInitialContext(); TopicReceive qr = new TopicReceive(); qr.init(ic, TOPIC); System.out .println("JMS Ready To Receive Messages (To quit, send a \"quit\" message)."); // Wait until a "quit" message has been received. synchronized (qr) { while (!qr.quit) { try { qr.wait(); } catch (InterruptedException ie) { ie.printStackTrace(); } } } qr.close(); } private static InitialContext getInitialContext() throws NamingException { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY); env.put(Context.PROVIDER_URL, PROVIDER_URL); return new InitialContext(env); } public String getEncoding() throws Exception { return "Hello World!"; } }
说明:点对点的消息只能被一个人消费一次,发布和订阅的消息可以被多个人消费一次。SendJMSFactory和SendJMSTopic等要通过JNDI从weblogic中查找......
评论
4 楼
hpyhgz
2011-12-26
例子不错。。
3 楼
wst0350
2011-09-01
感谢
2 楼
linliangyi2007
2009-02-16
好帖,代码清晰明了。
超级喜欢lz这样的教学贴,建议斑竹加精哦!
看了代码,一气呵成,感觉特爽快,一下子将jms的基础梳理了一遍,学习了!
希望楼主继续有jms深入的帖子发表
超级喜欢lz这样的教学贴,建议斑竹加精哦!
看了代码,一气呵成,感觉特爽快,一下子将jms的基础梳理了一遍,学习了!
希望楼主继续有jms深入的帖子发表
1 楼
liwei
2009-02-16
谢谢lz!正在学习中
相关推荐
Classes contained in javax.jms.jar: javax.transaction.xa.XAResource.class javax.jms.BytesMessage.class javax.jms.Message.class javax.jms.JMSException.class javax.jms.Destination.class javax.jms....
**JMS583芯片详解** JMS583是一款由JMicron科技公司设计的高性能USB 3.1 Gen 2至PCIe Gen3x2桥接芯片,旨在为存储设备提供高速数据传输能力。这款芯片是针对现代数据密集型应用而优化的,如SSD固态硬盘和外部存储...
`javax.jms.jar` 文件中包含了如`javax.jms.Queue`, `javax.jms.Topic`, `javax.jms.MessageProducer`, `javax.jms.MessageConsumer`, `javax.jms.ConnectionFactory`等关键接口,以及其他辅助类和异常类,开发者...
Java消息服务(Java Message Service,简称JMS)是Java平台中用于企业级应用间异步通信的标准接口。它提供了一种可靠的消息传递机制,允许应用程序创建、发送、接收和读取消息。`jms-1.1.jar` 是一个包含了JMS 1.1...
Java消息服务(Java Message Service,简称JMS)是Java平台中用于企业级应用集成的API,它定义了一组标准接口,允许应用程序创建、发送、接收和读取消息。`javax.jms`包是JMS规范的核心部分,包含了各种与消息传递...
**JMS583 USB转PCIE桥接芯片方案** JMS583是一款由JMicron科技公司设计的高效能USB到PCI Express (PCIe)桥接芯片,旨在为电子设备提供一种灵活的方式来实现高速数据传输。这款芯片能够将USB接口转换成PCIe接口,以...
《JMS 1.1 API详解与应用实践》 Java消息服务(Java Message Service,简称JMS)是Java平台中用于企业级应用间异步通信的一种标准接口。JMS 1.1是其一个重要的版本,它为分布式系统中的应用程序提供了一种可靠的...
标题中的“USB转SATA硬盘盒JMS583量产工具”是指一种专门用于将USB接口转换为SATA接口的硬件设备,其中JMS583是该转换器的主控芯片。这种硬盘盒允许用户将传统的SATA硬盘通过USB接口连接到电脑上,方便数据迁移、...
【JMS583量产工具】是一款专门针对JMS583芯片进行批量生产操作的软件工具,主要用于USB闪存盘的制作与修复。这款工具的全称可能为"JMS583 Mass Production Tool",它允许用户一次性对多个JMS583芯片驱动的USB设备...
javax.jms.BytesMessage.class javax.jms.Connection.class javax.jms.ConnectionConsumer.class javax.jms.ConnectionFactory.class javax.jms.ConnectionMetaData.class javax.jms.DeliveryMode.class javax.jms....
Java消息服务(Java Message Service,简称JMS)是Java平台中用于企业级应用集成的API,主要用于在分布式环境中传递消息。JMS1.1规范是该API的一个版本,提供了标准接口,使得不同的消息中间件(Message Oriented ...
标题中的“USB3.0移动硬盘盒 JMS578 固件2018最新固件, G-Technology 移动硬盘盒固件 for JMS578”揭示了这个压缩包文件主要关注的是一个特定型号的USB3.0移动硬盘盒,该盒子采用JMS578主控芯片,并且提供了2018年...
`javax.jms-1.1.jar` 是一个Java Message Service(JMS)的API库,它属于Java标准版(Java Standard Edition, Java SE)的一部分。JMS是Java平台上的一个规范,用于在分布式环境中进行异步消息传递。这个库主要用于...
文件包含: 13个不同固件 两个固件升级工具 其中一个是量产工具可以修改休眠时间和盒子信息 TRIM检查工具 有几个固件版本为全网首发,别人没有的资源 固件列表: JMS578_00.01.00.05 ...JMS578_254.02.03.09_NoUAS
WebLogic Server是一款由Oracle公司提供的企业级应用服务器,它支持Java Message Service (JMS) 规范,允许在分布式环境中可靠地发送和接收消息。JMS是Java平台上的标准接口,用于实现应用程序间的异步通信。本文将...
Weblogic JMS 依赖jar包是Oracle公司的中间件产品Weblogic Server中用于实现Java Message Service (JMS) 功能的关键组件。JMS是一种标准API,它允许应用程序在分布式环境中发送和接收消息,提供了异步通信的能力。在...
JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS(JMS
标题“JMS577主控通刷固件”指的是针对希捷移动硬盘中采用JMS577主控芯片的固件更新解决方案。在IT领域,固件是存储在硬件设备上的软件部分,它控制设备的操作并提供与主机系统的交互。JMS577是一种常见的硬盘主控...