gpt4 book ai didi

java - 从独立客户端向 JBoss 7.1 中的 JMS 主题发送消息,未找到 ConnectionFactory

转载 作者:行者123 更新时间:2023-11-30 09:07:43 25 4
gpt4 key购买 nike

我在从独立客户端向 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”,但仍然出现 namingException。这是我打开 ConnectionFactory 的 standalone.xml 片段:

 <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没有解决问题。提前谢谢你。

最佳答案

我相信您必须使用 RemoteConnectionFactory,因为您的客户端未在与 jboss 相同的虚拟机中运行。

应该对“jms/RemoteConnectionFactory”而不是“java:/ConnectionFactory”进行上下文查找

关于java - 从独立客户端向 JBoss 7.1 中的 JMS 主题发送消息,未找到 ConnectionFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23887178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com