我是JMS的新手,并且正在尝试加载connectionFactory。我遵循了一些教程,但是由于某种原因,它似乎无法正常工作,因此每次尝试“.lookup” connectionfactory时都会出现异常。
我正在使用JBoss7.1。
这是我尝试获取connectionFactory的类:
public class QueueSend extends Frame implements ActionListener {
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueSender qsender;
private Queue queue;
private TextMessage msg;
private TextField tf=new TextField();
private Button send=new Button("Send");
public QueueSend(){
super("Queue Sender");
setLocation(150,50);
setSize(200,100);
add(tf);
add(send,BorderLayout.SOUTH);
send.addActionListener(this);
send.setEnabled(false);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent w){
send("quit");
close();
System.exit(0);
}
});
setVisible(true);
init();
}
public void init(){
try{
InitialContext ctx = getInitialContext();
qconFactory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
queue = (Queue) ctx.lookup("java:/queue/text");
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
qsender = qsession.createSender(queue);
qcon.start();
send.setEnabled(true);
}catch(NamingException e1){e1.printStackTrace();
}catch(JMSException e2){e2.printStackTrace();}
}
public void actionPerformed(ActionEvent e){
String message=tf.getText();
tf.setText("");
if(message.length()>0)
send(message);
}
public void send(String message){
try{
//send a message with the given string
TextMessage textMessage = qsession.createTextMessage(message);
qsender.send(textMessage);
// enter text here
}catch(JMSException e){e.printStackTrace();}
}
public void close(){
try{
qsender.close();
qsession.close();
qcon.close();
}catch(JMSException e1){e1.printStackTrace();
}catch(NullPointerException e2){e2.printStackTrace();}
}
public static void main(String[] args){
new QueueSend();
}
public InitialContext getInitialContext(){
Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
properties.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT", "false");
properties.put("java.naming.provider.url","remote://127.0.0.1:4447");
properties.put("jboss.naming.client.ejb.context","true");
properties.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT","false" );
try {
return new InitialContext(properties);
} catch (NamingException e) {
System.out.println("Cannot generate InitialContext");
e.printStackTrace();
}
return null;
}
}
我得到这个异常(exception):
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.j ava:73)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
当我从浏览器登录到JBoss服务器时,在“命名”选项卡下找不到“ConnectionFactory”
这是来自standalone-full.xml:
<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>
我究竟做错了什么 ?
最佳答案
我发现了问题所在。
我的JBoss服务器的默认配置文件是standalone.xml,而在standalone-full.xml文件中配置了connectionFactory,这是standalone.xml的更完整版本,因此,我要做的就是更改从standalone.xml到standalone-full.xml的服务器,就完成了技巧:@)〜