我无法从独立客户端在 Linux 机器上的 JBoss 7.1 中将消息和对象发布到我的 JMS 主题实现。在这种情况下,我在 JUnit 测试用例中测试连接,这个:
@BeforeClass
public void setUp() {
tp = new FlightstatsConsumerTopic();
topic = new JMSTopicSender();
String client = "ettore";
String from = "JFK";
String to = "LHR";
String date = "2014/5/20";
sample = new FlightRequest(client, from, to, date);
}
@Test
public void testRequest() throws IOException, JMSException, NamingException {
System.out.println("TEST: testing flight request "+ sample.toString());
topic.sendObject(sample);
}
我只是想通过 JMS 主题发布一个对象,但无论如何我都会收到一个 NamingException 作为输出。
TEST: testing flight request FlightRequest [client=ettore, departure=JFK, arrival=LHR, date=2014/5/20, connectionType=DIRECT]
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[TopicPublisher] Remote Context initilized
javax.naming.NameNotFoundException: ConnectionFactory -- service jboss.naming.context.java.jboss.exported.ConnectionFactory
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:97)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:178)
at org.jboss.naming.remote.protocol.v1.Protocol$1.handleServerMessage(Protocol.java:127)
at org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1$MessageReciever$1.run(RemoteNamingServerV1.java:73)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
这是应该为 MyTopic 实现 Publisher 的类:
package commons;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/RequestSender")
public class JMSTopicSender extends HttpServlet {
/**
* This class sends a request to the JMS Topic
*/
// private static final Logger LOG =
// LoggerFactory.getLogger(JMSTopicSender.class);
private static final long serialVersionUID = 1L;
private TopicConnection conn = null;
private TopicSession session = null;
private Topic topic = null;
public static final String CONNECTION_FACTORY_NAME = "connection.factory.name";
public JMSTopicSender() {
super();
}
public InitialContext getInitialContext() {
Properties properties = new Properties();
properties.put(Context.SECURITY_PRINCIPAL, "guest");
properties.put(Context.SECURITY_CREDENTIALS, "guestpassw");
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jboss.naming.remote.client.InitialContextFactory");
properties.put(Context.PROVIDER_URL, "remote://localhost:4447");
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
properties.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT",
false);
properties.put("jboss.naming.client.ejb.context", true);
try {
return new InitialContext(properties);
} catch (NamingException e) {
System.out.println("Cannot generate InitialContext");
e.printStackTrace();
}
return null;
}
/**
* Set Up a Session with the JMS MyTopic
*
* @throws JMSException
* @throws NamingException
*/
public void setupPubSub() throws JMSException, NamingException {
InitialContext iniCtx = getInitialContext();
System.out.println("[TopicPublisher] Remote Context initilized");
try {
TopicConnectionFactory tmp = (TopicConnectionFactory) iniCtx
.lookup("java:/ConnectionFactory");
conn = tmp.createTopicConnection();
topic = (Topic) iniCtx.lookup("topic/MyTopic");
} catch (NamingException e1) {
e1.printStackTrace();
} catch (JMSException e2) {
e2.printStackTrace();
}
//System.out.println("[TopicPublisher] Lookup to ConnectionFactory done.");
//System.out.println("[TopicPublisher] Lookup to MyTopic done.");
session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
conn.start();
}
/**
* Send a message text to MyTopic
*
* @param text
* @throws JMSException
* @throws NamingException
*/
public void sendText(String text) throws JMSException, NamingException {
setupPubSub();
TopicPublisher send = session.createPublisher(topic);
TextMessage tm = session.createTextMessage(text);
send.publish(tm);
send.close();
}
/**
* Send an object as a Publisher into
*
* @param obj
* @throws JMSException
* @throws NamingException
*/
public void sendObject(Object obj) throws JMSException, NamingException {
setupPubSub();
TopicPublisher sender = session.createPublisher(topic);
ObjectMessage objMsg = session.createObjectMessage();
objMsg.setObject((Serializable) obj);
sender.send(objMsg);
}
/**
* Stop the connection and close
*
* @throws JMSException
*/
public void stop() throws JMSException {
conn.stop();
session.close();
conn.close();
}
当客户端尝试查找 ConnectionFactory 时捕获到异常,此处:
iniCtx.lookup("java:/ConnectionFactory");
我已经尝试过只使用“ConnectionFactory”,但仍然得到一个命名异常。这是我的standalone.xml 片段,它打开了 ConnectionFactory:
<subsystem xmlns="urn:jboss:domain:messaging:1.1">
<hornetq-server>
<persistence-enabled>true</persistence-enabled>
<journal-file-size>102400</journal-file-size>
<journal-min-files>2</journal-min-files>
<connectors>
<netty-connector name="netty" socket-binding="messaging"/>
<netty-connector name="netty-throughput" socket-binding="messaging-throughput">
<param key="batch-delay" value="50"/>
</netty-connector>
<in-vm-connector name="in-vm" server-id="0"/>
</connectors>
<acceptors>
<netty-acceptor name="netty" socket-binding="messaging"/>
<netty-acceptor name="netty-throughput" socket-binding="messaging-throughput">
<param key="batch-delay" value="50"/>
<param key="direct-deliver" value="false"/>
</netty-acceptor>
<in-vm-acceptor name="in-vm" server-id="0"/>
</acceptors>
<security-settings>
<security-setting match="#">
<permission type="send" roles="guest"/>
<permission type="consume" roles="guest"/>
<permission type="createNonDurableQueue" roles="guest"/>
<permission type="deleteNonDurableQueue" roles="guest"/>
</security-setting>
</security-settings>
<address-settings>
<address-setting match="#">
<dead-letter-address>jms.queue.DLQ</dead-letter-address>
<expiry-address>jms.queue.ExpiryQueue</expiry-address>
<redelivery-delay>0</redelivery-delay>
<max-size-bytes>10485760</max-size-bytes>
<address-full-policy>BLOCK</address-full-policy>
<message-counter-history-day-limit>10</message-counter-history-day-limit>
</address-setting>
</address-settings>
<jms-connection-factories>
<connection-factory name="InVmConnectionFactory">
<connectors>
<connector-ref connector-name="in-vm"/>
</connectors>
<entries>
<entry name="java:/ConnectionFactory"/>
</entries>
</connection-factory>
<connection-factory name="RemoteConnectionFactory">
<connectors>
<connector-ref connector-name="netty"/>
</connectors>
<entries>
<entry name="RemoteConnectionFactory"/>
<entry name="java:jboss/exported/jms/RemoteConnectionFactory"/>
</entries>
</connection-factory>
<pooled-connection-factory name="hornetq-ra">
<transaction mode="xa"/>
<connectors>
<connector-ref connector-name="in-vm"/>
</connectors>
<entries>
<entry name="java:/JmsXA"/>
</entries>
</pooled-connection-factory>
</jms-connection-factories>
<jms-destinations>
<jms-queue name="testQueue">
<entry name="queue/MyQueue"/>
</jms-queue>
<jms-topic name="testTopic">
<entry name="topic/MyTopic"/>
</jms-topic>
</jms-destinations>
</hornetq-server>
</subsystem>
<subsystem xmlns="urn:jboss:domain:logging:1.1">
我对 InitialContex 属性做错了吗?我已经看过一个类似的答案,但是通过启动 JBoss 切换到 sandalone-full.xml: sudo bash Standalone.sh -c Standalone-full.xml 并没有解决问题。先感谢您。