50分求解JMS问题
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://10.17.34.46: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
* @throws Exception
*/
public void onMessage(Message msg)
{
try {
String msgText;
if (msg instanceof TextMessage)
{
msgText = ((TextMessage)msg).getText();
}
else
{
msgText = msg.toString();
}
Mail mailHelper=new Mail();
mailHelper.sendMail("yangph@gsta.com", "skymiss18@163.com", "this is subject",msgText);
System.out.println("Message Received: "+ msgText );
if (msgText.equalsIgnoreCase("quit")) {
synchronized(this)
{
quit = true;
System.out.println("begin to notify all");
this.notifyAll(); // Notify main thread to quit
}
}
}
catch (JMSException jmse) {
jmse.printStackTrace();
} catch (SendFailedException e) {
// TODO 自动生成 catch 块
e.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 {
System.out.println("thread to wait....");
qr.wait();
}
catch (InterruptedException ie) {}
}
}
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);
}
}
我已经在WEBLOGIC配置了SendJMSQueue,但是查看WEBLOGIC SERVER的JNDI树时却没有看到SendJMSQueue,并且上面的程序运行的时候报错如下,:
javax.naming.NameNotFoundException: Unable to resolve 'SendJMSQueue' Resolved [Root exception is javax.naming.NameNotFoundException: Unable
esolve 'SendJMSQueue' Resolved ]; remaining name 'SendJMSQueue'
at weblogic.rjvm.BasicOutboundRequest.sendReceive()Lweblogic.rmi.spi.InboundResponse;(BasicOutboundRequest.java:108)
at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(Lweblogic.rmi.extensions.server.RemoteReference;Lweblogic.rmi.extensions.server.
imeMethodDescriptor;[Ljava.lang.Object;Ljava.lang.reflect.Method;)Ljava.lang.Object;(ReplicaAwareRemoteRef.java:290)
at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(Ljava.rmi.Remote;Lweblogic.rmi.extensions.server.RuntimeMethodDescriptor;[Ljava.
.Object;Ljava.lang.reflect.Method;)Ljava.lang.Object;(ReplicaAwareRemoteRef.java:247)
at weblogic.jndi.internal.ServerNamingNode_814_WLStub.lookup(Ljava.lang.String;Ljava.util.Hashtable;)Ljava.lang.Object;(Unknown Sourc
at weblogic.jndi.internal.WLContextImpl.lookup(Ljavax.naming.Name;Ljava.lang.String;)Ljava.lang.Object;(WLContextImpl.java:371)
at weblogic.jndi.internal.WLContextImpl.lookup(Ljava.lang.String;)Ljava.lang.Object;(WLContextImpl.java:359)
at javax.naming.InitialContext.lookup(Ljava.lang.String;)Ljava.lang.Object;(InitialContext.java:347)
at src.QueueReceive.init(Ljavax.naming.Context;Ljava.lang.String;)V(QueueReceive.java:96)
at src.QueueReceive.main([Ljava.lang.String;)V(QueueReceive.java:123)
Caused by: javax.naming.NameNotFoundException: Unable to resolve 'SendJMSQueue' Resolved
at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:858)
at weblogic.jndi.internal.BasicNamingNode.lookupHere(BasicNamingNode.java:230)
at weblogic.jndi.internal.ServerNamingNode.lookupHere(ServerNamingNode.java:154)
at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:188)
at weblogic.jndi.internal.RootNamingNode_WLSkel.invoke(Unknown Source)
at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:477)
at weblogic.rmi.cluster.ReplicaAwareServerRef.invoke(ReplicaAwareServerRef.java:108)
at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:420)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:353)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:144)
at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:415)
at weblogic.rmi.internal.BasicExecuteRequest.execute(BasicExecuteRequest.java:30)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)