关于JBOSS中JMS远程消费的问题?
大家好,今天测试EJB3.0的JMS相关内容,测试了一下远程消费,步骤和出现的异常如下:
1、写一个生产者:
package com.foshanshop.ejb3.app;
import java.util.Properties;
import javax.jms.BytesMessage;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.foshanshop.ejb3.bean.Man;
public class QueueSender {
public static void main(String[] args) {
QueueConnection conn = null;
QueueSession session = null;
try {
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
props.setProperty(Context.PROVIDER_URL, "10.1.35.126:1099");
props.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
InitialContext ctx = new InitialContext(props);
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");
conn = factory.createQueueConnection();
session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
Destination destination = (Queue) ctx.lookup("queue/foshanshop");
MessageProducer producer = session.createProducer(destination);
//发送文本
TextMessage msg = session.createTextMessage("佛山人您好,这是我的第一个消息驱动Bean");
producer.send(msg);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
session.close ();
conn.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
2、在JMS服务器上布置服务
<?xml version="1.0" encoding="UTF-8"?>
<server>
<mbean code="org.jboss.mq.server.jmx.Queue"
name="jboss.mq.destination:service=Queue,name=foshanshop">
<attribute name="JNDIName">queue/foshanshop </attribute>
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager </depends>
</mbean>
<mbean code="org.jboss.mq.server.jmx.Topic"
name="jboss.mq.destination:service=Topic,name=chatTopic">
<attribute name="JNDIName">topic/chatTopic </attribute>
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager </depends>
</mbean>
</server>
3、在另外一台机器上布置消费者(该机器也安装了JBOSS,打算将消费者布置在JBOSS里面)
package cn.tcl.remotefoshandrivebean;
import java.io.ByteArrayOutputStream;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.BytesMessage;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import cn.tcl.remotefoshandrivebean.bean.Man;
@MessageDriven(activationConfig =
{
@ActivationConfigProperty(propertyName="destinationType",
propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName="destination",
propertyValue="queue/foshanshop"),
@ActivationConfigProperty(propertyName="providerAdapterJNDI",
propertyValue="java:/RemoteJMSProvider")
})
public class FoshanConsumer implements MessageListener {
public void onMessage(Message msg) {
try {
if (msg instanceof TextMessage) {
TextMessage tmsg = (TextMessage) msg;
String content = tmsg.getText();
System.out.println(content);
}
} catch (Exception e){
e.printStackTrace();
}
}
}
同时在jboss的server\default\deploy目录下放一个配置文件foshanshop-consumer-service.xml文件内容如下:
<mbean code="org.jboss.jms.jndi.JMSProviderLoader"
name="jboss.mq:service=JMSProviderLoader,name=RemoteJMSProvider,server=remotehost">
<attribute name="ProviderName">RemoteJMSProvider </attribute>
<attribute name="ProviderAdapterClass">org.jboss.jms.jndi.JNDIProviderAdapter </attribute>
<!-- The connection factory -->
<attribute name="FactoryRef">UIL2XAConnectionFactory </attribute>
<!-- The queue connection factory -->
<attribute name="QueueFactoryRef">UIL2XAConnectionFactory </attribute>
<!-- The topic factory -->
<attribute name="TopicFactoryRef">UIL2XAConnectionFactory </attribute>
<!-- Connect to JNDI on the host "the-remote-host-name" port 1099-->
<attribute name="Properties">
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jnp.interfaces
java.naming.provider.url=10.1.35.126:1099
</attribute>
</mbean>
以上完成了所有的步骤,启动JMS服务器没有问题,启动消费者的JBOSS时系统报出如下异常:
20:40:57,165 WARN [ServiceController] Problem starting service jboss.j2ee:jar=M
essageDrivenBean.jar,name=FoshanConsumer,service=EJB3
javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interf
aces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException:
No ClassLoaders found for: org.jnp.interfaces.NamingContextFactory ]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:6
57)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288
)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.InitialContext. <init>(InitialContext.java:197)
at org.jboss.jms.jndi.JNDIProviderAdapter.getInitialContext(JNDIProvider
Adapter.java:44)
at org.jboss.ejb3.mdb.MessagingContainer.jmsCreate(MessagingContainer.ja
va:374)
at org.jboss.ejb3.mdb.MessagingContainer.innerStart(MessagingContainer.j
ava:166)
at org.jboss.ejb3.mdb.MessagingContainer.start(MessagingContainer.java:1
52)
at org.jboss.ejb3.mdb.MDB.start(MDB.java:126)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
从报出的异常上看Cannot instantiate class: org.jnp.interf
aces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException:
系统好象是没有找到NamingContextFactory这个类,而自己也查看了一下在jboss\server\default\lib这个目录下有jnpservler.jar这个包中存在,而这个目录下是自动加载,百思不得其解为什么会报出这个异常,希望各位JAVA高手能够指定
[解决办法]
你把%JBOSS_HOME%/client/jbossall-client.jar放在你的项目中
[解决办法]
楼主的问题已经解决了.我就只有顶一个,J个F了..
[解决办法]
应该就是缺少JAR包。
[解决办法]
jf
[解决办法]
谢谢了 楼主
[解决办法]
嘿嘿
jbossall-client包的路径
[解决办法]
将jboss中的client 目录中的jar包全部倒进工程去试试!