About Jboss AS 7 send/receive jms by queue
daemon0509 May 16, 2012 2:41 PMHello everybody~
I have a problem that I hope someone can help me or give some suggest.
I have a sample code which is about use jboss queue to implement jms, and I can use this sample code on Jboss 4.2.3 GA.
but when I change to use Jboss AS 7.1.0.CR1b or Jboss 7.1.1 final then this sample code can't works.
I found many solution and information from internet, but I still can't solve this problem.
Here are the error messages.
Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.naming.remote.client.InitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at SendRecvClient.setupPTP(SendRecvClient.java:60)
at SendRecvClient.sendRecvAsync(SendRecvClient.java:78)
at SendRecvClient.main(SendRecvClient.java:182)
Caused by: java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
... 7 more
and it is my sample code
import java.util.Hashtable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.jms.MapMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.Context;
import javax.jms.DeliveryMode;
import java.util.Date;
public class SendRecvClient
{
private final static String JNDI_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
public final static String JMS_FACTORY="jms/RemoteConnectionFactory";
QueueConnection qconn = null;
QueueSession qsession = null;
QueueSender qsend = null;
QueueReceiver qrecv = null;
Queue queue = null;
TextMessage Sendmessage = null;
String text = new String(" A text msg a;lsdoinyweoiuvtweryuvewvcygwelivrewv");
public static class ExListener implements MessageListener
{
public void onMessage(Message msg)
{
TextMessage Recvmessage = (TextMessage) msg;
try
{
System.out.println("onMessage, Recv text = " + Recvmessage.getText());
}
catch(Throwable t)
{
t.printStackTrace();
}
}
}
public void setupPTP() throws JMSException, NamingException
{
Hashtable<String, String> env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, "remote://localhost:4447");
env.put(Context.SECURITY_PRINCIPAL, "testuser");
env.put(Context.SECURITY_CREDENTIALS, "testpassword");
InitialContext iniCtx = new InitialContext(env);
Object tmp = iniCtx.lookup("jms/RemoteConnectionFactory");
QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;
qconn = qcf.createQueueConnection();
queue = (Queue) iniCtx.lookup("jms/queue/test");
qsession = qconn.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);
}
public void sendRecvAsync() throws JMSException, NamingException
{
System.out.println("Begin sendRecvAsync...");
// Set up the PTP connection, session
setupPTP();
// Set the async listener
qrecv = qsession.createReceiver(queue);
recv.setMessageListener(new ExListener());
// Send a text msg
qsend = qsession.createSender(queue);
Sendmessage = qsession.createTextMessage();
}
class Send extends Thread
{
public void run()
{
try
{
for(int i = 0; i < 10; i++)
{
Sendmessage.setText( text + " # " + i);
qsend.send(queue, Sendmessage);
System.out.println("Sent text = " + Sendmessage.getText());
}
}
catch(JMSException e)
{
System.out.print("Exception occurred:" + e.toString());
}
}
}
class Recv extends Thread
{
public void run()
{
try
{
qrecv.setMessageListener(new ExListener());
qconn.start();
}
catch(JMSException e)
{
}
}
}
public void run_thread1()
{
Send[] tt = new Send[1];
for(int i = 0; i < tt.length; i++)
{
tt[i] = new Send();
tt[i].start();
}
for(int i = 0; i < tt.length; i++)
{
try
{
tt[i].join();
}
catch(InterruptedException je)
{
je.printStackTrace();
}
}
}
public void run_thread2()
{
Recv[] kk = new Recv[10000];
for(int i = 0; i < kk.length; i++)
{
kk[i] = new Recv();
kk[i].start();
}
}
public void close() throws JMSException
{
qconn.close();
qsession.close();
qconn.close();
}
public static void main(String args[]) throws Exception, JMSException,NamingException
{
System.out.printf("Begin SendRecvClient,now =" + " %tF %<tT\n", new Date());
SendRecvClient client = new SendRecvClient();
client.sendRecvAsync();
client.run_thread1();
client.run_thread2();
client.close();
System.out.println("End SendRecvClient...");
System.out.printf("End SendRecvClient,now =" + " %tF %<tT\n", new Date());
}
}
Sorry, this is my first time to use Jboss application server.
I hope that anyone can help me or give me a suggest or direction to solve this problem.
thanks a lot.