我最近在学习jms。遇到了许多的问题!!在我配置完weblogic9.1之后这个程序就是不执行。不知道是为什么???谁知道是为什么呀???
//声明这个类定义在包examples.jms.queue中
package examples.jms.queue;
//声明这个类引入的其它类和包
import java.io.*;
import java.util.*;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;/** 这个实例演示怎样建立一个连接并到一个JMS队列接收消息。
* 这个目录中的例子操作同一个JMS队列。一起运行这些类观察消息发送和接收。
* 浏览队列中的消息。
*/
public class QueueReceive
implements MessageListener
{
/**
* 定义JNDI上下文构造器
*/
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
/**
* 定义JMS连接构造器
*/
public final static String JMS_FACTORY="weblogic.examples.jms.QueueConnectionFactory";
/**
* 定义队列
*/
public final static String QUEUE="weblogic.examples.jms.exampleQueue";
//声明队列连接构造器
private QueueConnectionFactory qconFactory;
//声明队列连接
private QueueConnection qcon;
//声明队列会话
private QueueSession qsession;
//声明队列接收
private QueueReceiver qreceiver;
//声明队列
private Queue queue;
private boolean quit = false;/**
* 消息监听接口
* @参数 msg 消息
*/ public void onMessage(Message msg)
{
try {
String msgText;
if (msg instanceof TextMessage) {
//获取文本消息的文本
msgText = ((TextMessage)msg).getText();
} else {
msgText = msg.toString();
} System.out.println("Message Received: "+ msgText );
if (msgText.equalsIgnoreCase("quit")) {
//退出运行消息
synchronized(this) {
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
} catch (JMSException jmse) {
jmse.printStackTrace();
}
} /**
* 创建所有从JMS队列接收消息必须的对象
*
* @参数 ctx JNDI上下文
* @参数 queueName 队列名
* @异常 NamingException 如果操作不能执行
* @异常 JMSException 由于内部错误,JMS 初始化失败抛出的异常
*/
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();
} /**
* 关闭JMS对象
* @异常 JMSException 由于内部错误,JMS不能关闭对象抛出的异常
*/
public void close()
throws JMSException
{
//关闭所有对象
qreceiver.close();
qsession.close();
qcon.close();
}
/**
* main() 方法
*
* @参数s args WebLogic服务器URL
* @异常 Exception 如果执行错误
*/ public static void main(String[] args)
throws Exception
{
if (args.length != 1) {
//打印用法提示
System.out.println("用法: java examples.jms.queue.QueueReceive WebLogicURL");
return;
}
//上下文初始化方法
InitialContext ic = getInitialContext(args[0]);
//本类实例
QueueReceive qr = new QueueReceive();
//调用初始化方法
qr.init(ic, QUEUE); System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message)."); // 等待,直到接收到"quit"消息
synchronized(qr) {
while (! qr.quit) {
try {
qr.wait();
} catch (InterruptedException ie) {}
}
}
qr.close();
}
//初始化方法
private static InitialContext getInitialContext(String url)
throws NamingException
{
//属性对象
Hashtable env = new Hashtable();
//设置属性
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}}
//声明这个类定义在包examples.jms.queue中
package examples.jms.queue;
//声明这个类引入的其它类和包
import java.io.*;
import java.util.*;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;/** 这个实例演示怎样建立一个连接并到一个JMS队列接收消息。
* 这个目录中的例子操作同一个JMS队列。一起运行这些类观察消息发送和接收。
* 浏览队列中的消息。
*/
public class QueueReceive
implements MessageListener
{
/**
* 定义JNDI上下文构造器
*/
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
/**
* 定义JMS连接构造器
*/
public final static String JMS_FACTORY="weblogic.examples.jms.QueueConnectionFactory";
/**
* 定义队列
*/
public final static String QUEUE="weblogic.examples.jms.exampleQueue";
//声明队列连接构造器
private QueueConnectionFactory qconFactory;
//声明队列连接
private QueueConnection qcon;
//声明队列会话
private QueueSession qsession;
//声明队列接收
private QueueReceiver qreceiver;
//声明队列
private Queue queue;
private boolean quit = false;/**
* 消息监听接口
* @参数 msg 消息
*/ public void onMessage(Message msg)
{
try {
String msgText;
if (msg instanceof TextMessage) {
//获取文本消息的文本
msgText = ((TextMessage)msg).getText();
} else {
msgText = msg.toString();
} System.out.println("Message Received: "+ msgText );
if (msgText.equalsIgnoreCase("quit")) {
//退出运行消息
synchronized(this) {
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
} catch (JMSException jmse) {
jmse.printStackTrace();
}
} /**
* 创建所有从JMS队列接收消息必须的对象
*
* @参数 ctx JNDI上下文
* @参数 queueName 队列名
* @异常 NamingException 如果操作不能执行
* @异常 JMSException 由于内部错误,JMS 初始化失败抛出的异常
*/
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();
} /**
* 关闭JMS对象
* @异常 JMSException 由于内部错误,JMS不能关闭对象抛出的异常
*/
public void close()
throws JMSException
{
//关闭所有对象
qreceiver.close();
qsession.close();
qcon.close();
}
/**
* main() 方法
*
* @参数s args WebLogic服务器URL
* @异常 Exception 如果执行错误
*/ public static void main(String[] args)
throws Exception
{
if (args.length != 1) {
//打印用法提示
System.out.println("用法: java examples.jms.queue.QueueReceive WebLogicURL");
return;
}
//上下文初始化方法
InitialContext ic = getInitialContext(args[0]);
//本类实例
QueueReceive qr = new QueueReceive();
//调用初始化方法
qr.init(ic, QUEUE); System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message)."); // 等待,直到接收到"quit"消息
synchronized(qr) {
while (! qr.quit) {
try {
qr.wait();
} catch (InterruptedException ie) {}
}
}
qr.close();
}
//初始化方法
private static InitialContext getInitialContext(String url)
throws NamingException
{
//属性对象
Hashtable env = new Hashtable();
//设置属性
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货