我在同一台机器上运行 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)
有人可以指导我吗...