【发布时间】:2013-02-18 07:42:06
【问题描述】:
我在同一台机器上运行 jboss5 和 jboss 7,所以我更改了端口号(jboss5-4040 和 jboss7-8080)。我需要从 jboss 5 向 jboss 7 发送消息。我读了一篇文章,说必须使用 JMS 桥接器。
https://community.jboss.org/wiki/BridgeJMSMessagesFromAS5ToAS7
我在 jboss 5 中创建了一个队列,并通过独立程序向队列发送消息。
import java.io.Console;
import java.util.Properties;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class sender {
String url_;
String name_;
Connection conn = null;
Session session = null;
Queue queue = null;
public sender(String url, String name) throws JMSException,
NamingException {
url_ = url;
name_ = name;
this.initializeSender();
}
private void initializeSender() throws JMSException, NamingException {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
props.setProperty("java.naming.provider.url", "jnp://localhost:1099");
Context context = new InitialContext(props);
ConnectionFactory cf = (ConnectionFactory) context
.lookup("java:/ConnectionFactory");
conn = cf.createConnection();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) context.lookup(name_);
conn.start();
}
public void send(String text) throws JMSException, NamingException {
// Send a text msg
MessageProducer producer = session.createProducer(queue);
TextMessage tm = session.createTextMessage(text);
producer.send(tm);
producer.close();
}
public void disconnect() throws JMSException {
if (conn != null) {
conn.stop();
}
if (session != null) {
session.close();
}
if (conn != null) {
conn.close();
}
}
public String getTopicName() {
return name_;
}
public String getTopicURL() {
return url_;
}
public static void main(String args[]) throws Exception {
sender sender = new sender("remote://localhost:4447", "BalaQueue");
sender.send("My Message");
sender.disconnect();
}
}
我在 jboss 7 中创建了一个队列,并通过独立程序向队列发送消息。
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
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;
public class QSender {
public static void main(String[] args) {
new QSender().send();
}
public void send() {
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
try {
// Strings for JNDI names
String factoryName = "jms/RemoteConnectionFactory";
String queueName = "jms/queue/ram";
// Create an initial context.
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jboss.naming.remote.client.InitialContextFactory");
props.put(Context.PROVIDER_URL, "remote://localhost:4447");
props.put(Context.SECURITY_PRINCIPAL, "ramguest");
props.put(Context.SECURITY_CREDENTIALS, "password");
InitialContext context = new InitialContext(props);
QueueConnectionFactory factory = (QueueConnectionFactory) context
.lookup(factoryName);
Queue queue = (Queue) context.lookup(queueName);
context.close();
// Create JMS objects
QueueConnection connection = factory.createQueueConnection(
"ramguest", "password");
QueueSession session = connection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
QueueSender sender = session.createSender(queue);
System.out.println("Enter message to send or 'quit' to quit.");
String messageText = null;
while (true) {
messageText = reader.readLine();
if ("quit".equalsIgnoreCase(messageText)) {
break;
}
TextMessage message = session.createTextMessage(messageText);
sender.send(message);
}
// Exit
reader.close();
System.out.println("Exiting...");
connection.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
还有我的桥接文件(jboss-service.xml)
<server>
<loader-repository>com.example:archive=unique-archive-name
<loader-repository-config>java2ParentDelegation=false</loader-repository-config>
</loader-repository> <!-- AS7 JMS Provider -->
<mbean code="org.jboss.jms.jndi.JMSProviderLoader" name="jboss.messaging:service=JMSProviderLoader,name=RemoteJBossMQProvider"> <attribute name="ProviderName">RemoteXAConnectionFactory</attribute>
<attribute name="ProviderAdapterClass">org.jboss.jms.jndi.JNDIProviderAdapter</attribute>
<attribute name="FactoryRef">jms/RemoteConnectionFactory</attribute>
<attribute name="QueueFactoryRef">jms/RemoteConnectionFactory</attribute>
<attribute name="TopicFactoryRef">jms/RemoteConnectionFactory</attribute>
<attribute name="Properties"> java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=remote://localhost:4447
java.naming.security.principal=ramguest
java.naming.security.credentials=password
</attribute>
</mbean>
<mbean code="org.jboss.jms.server.bridge.BridgeService" name="jboss.jms:service=Bridge,name=LegayBridgeSend" xmbean-dd="xmdesc/Bridge-xmbean.xml">
<depends optional-attribute-name="SourceProviderLoader">jboss.messaging:service=JMSProviderLoader,name=JMSProvider</depends>
<depends optional-attribute-name="TargetProviderLoader">jboss.messaging:service=JMSProviderLoader,name=RemoteJBossMQProvider</depends>
<attribute name="SourceDestinationLookup">BalaQueue</attribute>
<attribute name="TargetDestinationLookup">java:jboss/exported/jms/queue/ram</attribute>
<attribute name="QualityOfServiceMode">1</attribute>
<attribute name="MaxBatchSize">1</attribute>
<attribute name="MaxBatchTime">-1</attribute>
<attribute name="FailureRetryInterval">10000</attribute>
<attribute name="MaxRetries">-1</attribute>
<attribute name="AddMessageIDInHeader">false</attribute>
<attribute name="TargetUsername">ramguest</attribute>
<attribute name="TargetPassword">password</attribute>
</mbean>
</server>
当我启动 jboss5 时出现异常
WARN [Bridge] jboss.jms:name=LegayBridgeSend,service=Bridge Failed to set up connections
javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out]
at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1678)
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1795)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:693)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:686)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at org.jboss.jms.server.bridge.JNDIFactorySupport.createObject(JNDIFactorySupport.java:66)
at org.jboss.jms.server.bridge.JNDIDestinationFactory.createDestination(JNDIDestinationFactory.java:45)
at org.jboss.jms.server.bridge.Bridge.setupJMSObjects(Bridge.java:953)
at org.jboss.jms.server.bridge.Bridge.setupJMSObjectsWithRetry(Bridge.java:1223)
at org.jboss.jms.server.bridge.Bridge.access$1600(Bridge.java:68)
at org.jboss.jms.server.bridge.Bridge$FailureHandler.run(Bridge.java:1569)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.net.SocketTimeoutException: Receive timed out
at java.net.PlainDatagramSocketImpl.receive0(Native Method)
at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:145)
at java.net.DatagramSocket.receive(DatagramSocket.java:725)
at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1647)
谁能指导我...
【问题讨论】:
-
使用 REST Web 服务要容易得多。 RestEeasy 的实现就像它的名字一样简单。在 jBoss 7 quickstarter 项目集中有几个例子如何建立一个 rest 服务以及如何调用它。相信在 jBoss 5 方面做同样的事情并不难。
-
我应该只根据要求使用 JMS 执行此操作..
-
在两台服务器上添加传出和传入队列,并让发送者休息客户端从传出队列读取消息,将它们发布到另一台服务器的 Web 服务,该服务会将消息添加到其传入队列,反之亦然。这将重新创建 jms 桥,同时保持两台服务器与人类可读的传输机制解耦
-
在 jboss-service.xml 中,我添加了 SourceDestinationLookup 和 TargetDestinationLookup。这些是传入和传出队列。但是现在上述错误已经解决并引发了一些新错误“javax.naming.NamingException:无法创建远程连接[根异常是java.util.ServiceConfigurationError:org.xnio.XnioProvider:Provider org .xnio.nio.NioXnioProvider 无法实例化:java.lang.NoClassDefFoundError: 无法初始化类 org.xnio.nio.NioXnioProvider]" org.xnio.nio.NioXnioProvider 类在类路径中也可用
标签: jboss jms jboss7.x jboss5.x