/**
* 接收
* */
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();
}
}
}
}
解决方案 »
- 这段代码哪儿有问题,求解释。
- Hibernate Oracle序列自动增长问题
- HQL多表查询
- 请前辈指点一二!
- Spring 2 + Hibernate 3 事务整合问题,高手请进
- 为什么我用struts2 加入验证之后就不执行Action中的方法啊?struts2 高手请进!!!!!!!!!!
- 如何写一个blog首页?roller weblogger相关
- 送分题!
- webcontain中两个javabean如何通信?
- struts2拦截器获取HTTPsession失败
- 请教以下情况怎么用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();
}
}
}
}