Java Jboss客户端向Jboss中的远程hornetq发送消息
我在Java Jboss客户端向Jboss中的远程hornetq发送消息,java,jboss,jndi,hornetq,Java,Jboss,Jndi,Hornetq,我在JBoss(JB\u客户端)中运行了一个客户端,它需要将消息发送到远程JBoss服务器(JB\u服务器)HornetQ。远程jboss服务器(JB_server)需要将响应消息发送回它的HornetQ。因此,JB_ClientMDB正在收听remoteHorentQ以获取响应 我正在为我的客户机和服务器使用JBossAS6。该系统在本地环境下运行良好,即客户端和服务器都在同一个Jboss中。但现在我需要将客户机和服务器分成两台机器 这是我的测试客户机配置 Properties
JBoss
(JB\u客户端)中运行了一个客户端,它需要将消息发送到远程JBoss服务器(JB\u服务器)HornetQ
。远程jboss服务器(JB_server)需要将响应消息发送回它的HornetQ
。因此,JB_ClientMDB
正在收听remoteHorentQ
以获取响应
我正在为我的客户机和服务器使用JBossAS6。该系统在本地环境下运行良好,即客户端和服务器都在同一个Jboss中。但现在我需要将客户机和服务器分成两台机器
这是我的测试客户机配置
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
prop.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
prop.put(Context.PROVIDER_URL, "jnp://localhost:1099");
ictx = new InitialContext(prop);
conFactory = (ConnectionFactory)ictx.lookup("/ConnectionFactory");
qcon = (QueueConnection)conFactory.createConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
除定义的队列外,服务器没有特殊设置
我需要在服务器端和客户端进行什么配置才能使其正常工作?我能够连接到远程服务器并从远程队列获得响应。我的客户端和服务器都运行在两台独立的计算机JBossAS[6.1.0.Final“Neo”]。 我的客户端是一个简单的web应用程序(war文件) 客户端队列发送方类。
private static void prepareContext() {
logger.debug("Loading Context");
try {
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
prop.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
prop.put(Context.PROVIDER_URL, "jnp://10.1.4.48:1099");
ictx = new InitialContext(prop);
conFactory = (ConnectionFactory)ictx.lookup("/ConnectionFactory");
qcon = (QueueConnection)conFactory.createConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
}
catch (NamingException er) {
logger.fatal("Error ", er);
}
catch (JMSException er) {
logger.fatal("Error ", er);
}
}
public static boolean sendToQueue(String xml, String sendQ) {
logger.warn("Sending to queue: " + xml);
try {
prepareContext();
Queue queue = getQueue(sendQ);
if (null == queue) {
throw new JMSException("Queue not defined at the end point");
}
qcon.start();
QueueSender qsender = qsession.createSender(queue);
TextMessage tmsg = qsession.createTextMessage();
tmsg.setText(xml);
qsender.send(tmsg);
return true;
}
catch (JMSException er) {
logger.fatal("Error ", er);
}
finally {
try { qsession.close(); } catch (Exception er) {/**/}
try { qcon.close(); } catch (Exception er) {/**/}
}
return false;
}
上面是客户端发送方代码
现在让我们看看客户端消息接收器是如何工作的。
我使用MDB
监听远程服务器队列
public class MessageReceiverBean implements MessageListener {
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage textMsg = (TextMessage) message;
logger.info("Inside onMessage of client app MessageReceiverBean : " + textMsg.getText());
}
}
catch (Exception er) {
logger.error("Error while retrieving message from Service provider", er);
}
}
}
MDB配置在我的war文件的META-INF文件夹中的ejb-jar.xml中
<message-driven>
<ejb-name>User1</ejb-name>
<ejb-class>com.my.MessageReceiverBean</ejb-class>
<messaging-type>javax.jms.MessageListener</messaging-type>
<transaction-type>Container</transaction-type>
<activation-config>
<activation-config-property>
<activation-config-property-name>destinationType</activation-config-property-name>
<activation-config-property-value>javax.jms.Queue</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>destination</activation-config-property-name>
<activation-config-property-value>queue/User4</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>ConnectorClassName</activation-config-property-name>
<activation-config-property-value>org.hornetq.core.remoting.impl.netty.NettyConnectorFactory</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>ConnectionParameters</activation-config-property-name>
<activation-config-property-value>host=10.1.4.48;port=5445</activation-config-property-value>
</activation-config-property>
</activation-config>
</message-driven>
如果您检查您的hornetq jms.xml
,您可以在上面的
下看到mappedName已定义(
)。
这是您应该在EJB中查找的JNDI名称,以便在远程HornetQ服务器中找到connectionFactory。如果您需要使用EJBCDI注入而不是JNDI查找,就可以这样做
重新启动客户端jboss,然后您应该能够将消息从客户端发送到服务器。(启动jboss时没有特殊设置)
如果您转到客户端的jmx控制台并转到org.hornetq
,然后单击module=JMS,name=“NettyConnectionFactory”,type=ConnectionFactory
,您可以验证它连接在哪里。在StaticConnectors
中,您可以看到此NettyConnector正在连接的位置
现在服务器端Jboss HornetQ设置。
private static void prepareContext() {
logger.debug("Loading Context");
try {
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
prop.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
prop.put(Context.PROVIDER_URL, "jnp://10.1.4.48:1099");
ictx = new InitialContext(prop);
conFactory = (ConnectionFactory)ictx.lookup("/ConnectionFactory");
qcon = (QueueConnection)conFactory.createConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
}
catch (NamingException er) {
logger.fatal("Error ", er);
}
catch (JMSException er) {
logger.fatal("Error ", er);
}
}
public static boolean sendToQueue(String xml, String sendQ) {
logger.warn("Sending to queue: " + xml);
try {
prepareContext();
Queue queue = getQueue(sendQ);
if (null == queue) {
throw new JMSException("Queue not defined at the end point");
}
qcon.start();
QueueSender qsender = qsession.createSender(queue);
TextMessage tmsg = qsession.createTextMessage();
tmsg.setText(xml);
qsender.send(tmsg);
return true;
}
catch (JMSException er) {
logger.fatal("Error ", er);
}
finally {
try { qsession.close(); } catch (Exception er) {/**/}
try { qcon.close(); } catch (Exception er) {/**/}
}
return false;
}
在服务器Jboss中编辑hornetq-configuration.xml并更改该部分
<acceptor name="netty">
<factory-class>org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory</factory-class>
<param key="host" value="${jboss.esb.bind.address:10.1.4.48}"/> <!-- Here I add the server IP and esb. May be you can remove the esb and use "jboss.bind.address:10.1.4.48" -->
<param key="port" value="${hornetq.remoting.netty.port:5445}"/>
</acceptor>
org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory
这就是服务器Jboss设置。我用run.bat-b10.1.4.48选项启动服务器
希望这有帮助