/**
* 接收
* */
public void receive(){ Context context = null;
QueueConnection queueConnection = null;
QueueConnectionFactory queuefactory = null;
QueueSession queueSession = null;
Queue queue = null;
QueueReceiver queueReceiver = null;
try {
Hashtable properties = new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.exolab.jms.jndi.InitialContextFactory");
properties.put(Context.PROVIDER_URL,"rmi://localhost:1099/"); context = new InitialContext(properties);
queuefactory = (QueueConnectionFactory)context.lookup("JmsQueueConnectionFactory");
queueConnection = queuefactory.createQueueConnection();
//启动接收队列线程
queueConnection.start();
//产生队列Session,设置事务为false,自动应答消息接收
queueSession =queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
//获得默认内建在JMS里的队列之一:queue1
queue = (Queue) context.lookup("queue1");
//产生JMS队列接收器
queueReceiver = queueSession.createReceiver(queue);
//通过同步的方法接收消息
Message message = queueReceiver.receive();
if (message instanceof TextMessage){
String messageText = ((TextMessage) message). getText();
System.out.println(messageText);
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(queueReceiver != null){
try {
queueReceiver.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(queueSession != null){
try {
queueSession.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(queueConnection != null){
try {
queueConnection.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// close the context
if (context != null) {
try {
context.close();
} catch (NamingException exception) {
exception.printStackTrace();
}
}
}
}
解决方案 »
- 如何用STRING给MYBATIS配置文件传值(在线等,着急)
- 如何使用java将cgm转换成pdf文件?求大侠相助
- Spring 如何读取 jar包中的 applicationContext-*.xml文件 很郁闷啊 搞了我两天了
- Error From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1:10.5.1 500 Internal Server Error 200分解决问题
- 违反完整约束条件 (XWSERVER.FK_ORDERS_CAR) - 未找到父项
- 冬天裸体求答案。。。。
- 为什么这段代码在eclipse里面编译就通不过??
- 如何使用JNDI访问ms Active Directory?
- hibernate hbm异常An association from the table refers to an unmapped class
- 在weblogic8.1上,struts jsp代码中标签bean:define无效。
- 请教以下情况怎么用DTD语法表示?
- 一个rmi问题:服务器对象的激活示例,运行ProductActivator.class报错
/**
* 浏览
* */
public void browser(){
Context context = null;
QueueConnection queueConnection = null;
QueueConnectionFactory queuefactory = null;
QueueSession queueSession = null;
Queue queue = null;
QueueBrowser queuebrowser = null;
try {
Hashtable properties = new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.exolab.jms.jndi.InitialContextFactory");
properties.put(Context.PROVIDER_URL,"rmi://localhost:1099/"); context = new InitialContext(properties);
queuefactory = (QueueConnectionFactory)context.lookup("JmsQueueConnectionFactory");
queueConnection = queuefactory.createQueueConnection();
//启动接收队列线程
queueConnection.start();
//产生队列Session,设置事务为false,自动应答消息接收
queueSession =queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
//获得默认内建在JMS里的队列之一:queue1
queue = (Queue) context.lookup("queue1");
//创建queuebrowser
//queuebrowser = queueSession.createBrowser(queue);
queuebrowser = queueSession.createBrowser(queue,"JMSPriority=4");
Enumeration messages = queuebrowser.getEnumeration();
while (messages.hasMoreElements()) {
Message message = (Message) messages.nextElement();
if (message instanceof TextMessage) {
TextMessage text = (TextMessage) message;
System.out.println("Browsed: " + text.getJMSMessageID());
System.out.println("相互关系, 相关 CorrelationID:"+text.getJMSCorrelationID());
System.out.println("发送类型 DeliveryMode:"+text.getJMSDeliveryMode());
System.out.println("满期, 呼出, 呼气 Expiration:"+text.getJMSExpiration());
System.out.println(" 优先权 JMSPriority:"+text.getJMSPriority());
System.out.println("再要求 Redelivered:"+text.getJMSRedelivered());
System.out.println("时间表Timestamp:"+text.getJMSTimestamp());
System.out.println("Type:"+text.getJMSType());
System.out.println("答复ReplyTo:"+text.getJMSReplyTo());
System.out.println("消息发送的目的地 Destination:"+text.getJMSDestination());
System.out.println("text:"+text.getText());
System.out.println("---------------------------------");
} else if (message != null) {
System.out.println("Browsed non text message");
}
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(queuebrowser != null){
try {
queuebrowser.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(queueSession != null){
try {
queueSession.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(queueConnection != null){
try {
queueConnection.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// close the context
if (context != null) {
try {
context.close();
} catch (NamingException exception) {
exception.printStackTrace();
}
}
}
}