Java 在 JMS 上找不到带有 JNDI 的 ConnectionFactory

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21026300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:03:06  来源:igfitidea点击:

Cannot find ConnectionFactory with JNDI on JMS

javajbossjmsjndi

提问by javaLovah

I'm new to JMS , and I'm trying to load a connectionFactory. I followed a couple of tutorials , but for some reason it doesn't seem to work , I get an exception every time I try to '.lookup' connectionfactory. I'm using JBoss7.1.

我是 JMS 新手,我正在尝试加载 connectionFactory。我遵循了几个教程,但由于某种原因它似乎不起作用,每次尝试“.lookup”连接工厂时都会出现异常。我正在使用 JBoss7.1。

Here is the class from where I'm trying to get the connectionFactory:

这是我试图从中获取 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;
   }
}

I get this 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.handleServerMessage(Protocol.java:127)
at  org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1$MessageReciever.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)

When i log into JBoss server from the browser , I can't find 'ConnectionFactory' under the 'naming' tab

当我从浏览器登录 JBoss 服务器时,在“命名”选项卡下找不到“ConnectionFactory”

this is from the standalone-full.xml:

这是来自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>

What am i doing wrong ?

我究竟做错了什么 ?

采纳答案by javaLovah

I found out what the problem was. The default configuration file for my JBoss server was standalone.xml while the connectionFactory is configured in the standalone-full.xml file which is a more full version of the standalone.xml, So all I had to do was change the configuration file of the server from standalone.xml to standalone-full.xml and that did the trick :@)~

我发现了问题所在。我的 JBoss 服务器的默认配置文件是 standalone.xml 而 connectionFactory 是在 standalone-full.xml 文件中配置的,这是一个更完整的 standalone.xml 版本,所以我所要做的就是更改服务器从standalone.xml 到standalone-full.xml 就成功了:@)~

回答by Jon Onstott

For me, I was trying to look up java:jboss/exported/jms/RemoteConnectionFactoryin on the remote Widlfly. Instead of using java:/jms/RemoteConnectionFactorywhich was wrong, I needed to look up jms/RemoteConnectionFactory(the text after exported).

对我来说,我试图仰望java:jboss/exported/jms/RemoteConnectionFactory遥远的 Widlfly。而不是使用java:/jms/RemoteConnectionFactorywhich 是错误的,我需要查找jms/RemoteConnectionFactory(后面的文本exported)。