我试图在JBoss 6.1.0(Java EE 6 - Hornetq)上运行一个运行MDB的简单Web应用程序来尝试JMS和MDB。 我还需要一个简单的独立客户端,它在MDB正在侦听的队列上发送文本消息。 这只是一次尝试,所以客户端发送一个简单的“你好世界”就足够了!"文本消息和MDB只是读取它并将其写入日志。 我在互联网上发现了很多例子,但有人使用Glassfish作为服务器,另外还没有解释导入客户端的正确库是什么,另外还没有解释如何执行队列和连接工厂查找时客户端是远程的,所以我仍然无法实现正常运行的应用程序。
以下是我创建的MDB的代码:
package it.test.mymdb;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@messageDriven(
activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType",
propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination",
propertyValue = "myQueue")})
public class MyMDB implements MessageListener {
private static final Logger logger = LoggerFactory.getLogger(MyMDB.class);
@Override
public void onMessage(Message message) {
try {
logger.info("Messaggio ricevuto");
TextMessage txtMsg = (TextMessage) message;
logger.info(txtMsg.getText());
}
catch (JMSException ex) {
logger.error("Errore durante la lettura del testo del messaggio.");
}
}
}
这是我定义队列和连接工厂的hornetq-jms.xml文件:
<?xml version="1.0"?>
<configuration xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:hornetq">
<connection-factory name="myConnectionFactory">
<connectors>
<connector-ref connector-name="netty"/>
</connectors>
<entries>
<entry name="/myConnectionFactory"/>
</entries>
</connection-factory>
<queue name="myQueue">
<entry name="/queue/myQueue" />
</queue>
</configuration>
问题1:我是否要在此文件中插入&#34; user&#34;和#34;密码&#34;客户端执行JNDI搜索所需的数据?如果是,怎么样?
我的MDB部署似乎有效,如果我访问JBoss管理面板,我会看到队列:
Name: myQueue
JNDI Binding: /queue/myQueue
Address: jms.queue.myQueue
Temporary: false
Paused: false
和连接工厂:
Name: myConnectionFactory
JNDI Bindings: /myConnectionFactory
这是我客户的(不工作)代码:
package it.test.mymdbclient;
import java.util.Hashtable;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class MyMdbClient {
private final static String JNDI_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
private final static String JMS_FACTORY = "myConnectionFactory";
private final static String QUEUE = "queue/myQueue";
private final static String jbossUrl = "remote://localhost:4447";
private static InitialContext getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, jbossUrl);
env.put(Context.SECURITY_PRINCIPAL, "admin"); //PROBABLY THOSE CREDENTIALS ARE NOT CORRECT
env.put(Context.SECURITY_CREDENTIALS, "admin");
return new InitialContext(env);
}
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext();
QueueConnectionFactory qconFactory = (QueueConnectionFactory) ic.lookup(JMS_FACTORY);
QueueConnection qcon = qconFactory.createQueueConnection("admin", "admin"); //PROBABLY THOSE CREDENTIALS ARE NOT CORRECT
QueueSession qsession = qcon.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) ic.lookup(QUEUE);
QueueSender qsender = qsession.createSender(queue);
qcon.start();
TextMessage msg = qsession.createTextMessage();
msg.setText("Hello world! :)");
qsender.send(msg);
qsender.close();
qsession.close();
qcon.close();
}
}
这是我导入的库,但我几乎可以肯定它是不够的,这是引发异常的原因。如果它是我需要的其他图书馆吗?
<dependency>
<groupId>org.hornetq</groupId>
<artifactId>hornetq-jms-client</artifactId>
<version>2.3.9.Final</version>
</dependency>
这是我运行客户端时抛出的异常:
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(NamingManager.java:674)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.init(InitialContext.java:244)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
at it.test.mymdbclient.MyMdbClient.getInitialContext(MyMdbClient.java:27)
at it.test.mymdbclient.MyMdbClient.main(MyMdbClient.java:31)
Caused by: java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:672)
... 5 more
答案 0 :(得分:1)
异常正在发生,因为您忘记添加&#34; jboss-client.jar &#34;它位于jboss文件夹中:
&#34; jbdevstudio \运行时\的JBoss-EAP \ BIN \客户\&#34;
这应该有效,并记得在运行客户端之前启动服务器
答案 1 :(得分:0)
看到这[link] [1]和[this] [2],这是同样的错误,你需要将jar放入classpath服务器。我希望有所帮助。