5 Replies Latest reply on Aug 27, 2013 7:54 AM by xiang yingbing

    JMS Queue Lookup

    akash bansal Newbie

      Hi,

       

      I have two jboss 7.2 servers as per following configuration:

      1)      JBM-1 (which has following Queue and ConnectionFactory configured)

                fc/errorQueue (JNDI Name is java:jboss/exported/fc/errorQueue)

                fc/RemoteConnectionFactory   (JNDI Name is java:jboss/exported/fc/RemoteConnectionFactory)

       

      2)     JBM-2

                This JBoss instance has one EAR deployed in which I want to lookup the ConnectionFactory as well as Queue deployed on JBM-1 instance. I am using following code of snippet for the same:

               

                Properties env = new Properties();

                env.put(Context.PROVIDER_URL, "remote://JBM-1:4447");

                env.put(Context.SECURITY_PRINCIPAL, "user1");

                env.put(Context.SECURITY_CREDENTIALS, "password");

                InitialContext ctx = new InitialContext(env);

                QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("fc/RemoteConnectionFactory");

                Queue queue = (Queue) ctx.lookup("fc/errorQueue");

       

      But what is happening it is trying to lookup the Connection Factory and Queue on JBM-2 JBoss instance rather than on JBM-1 instance and is throwing javax.naming.NameNotFoundException.

       

      Pls. help me.

        • 1. Re: JMS Queue Lookup
          xiang yingbing Master

          Have you tried to change JBM-1 to the real IP of JBM-1?

          Are you sure the two JBoss server are in same cluster?

          • 2. Re: JMS Queue Lookup
            akash bansal Newbie

            Hi,

            I have tried JBM-1 with the real IP address but it is not working. And both the serves i.e. JBM-1 and JBM-2 are different and on different physical machine and they do not participate in clustering.

            • 3. Re: JMS Queue Lookup
              xiang yingbing Master

              If the two servers are NOT in same cluster, I think you had better use real IP instead of name.

               

              Since you are trying to get a connection to "ANY" Server, I think bellow PURE CLIENT code(which can run in both server and client) is useful to you (It works for me):

               

              
              
              
              
              private static ConnectionFactory jmsConnectionFactory = null;
              
              
              
              //缓存的共享的JMS资源,最后需要关闭
              
              
              
              private static Connection  
              jmsConnection = null;
              
              
              
              private static Session     
              jmsSession = null;
              
              
              
              //缓存的 nmsSOE JMS资源,MessageConsumer/MessageProducer需要关闭
              
              
              
              private static Destination 
              jmsDestination_topic_nmsSOE = null;
              
              
              
              private static MessageConsumer jmsConsumer_topic_nmsSOE = null;
              
              
              
              //缓存的 test   JMS资源,MessageConsumer/MessageProducer需要关闭
              
              
              
              private static Destination 
              jmsDestination_queue_test = null;
              
              
              
              private static MessageConsumer jmsConsumer_queue_test = null;
              
              
              
              private static MessageProducer jmsProducer_queue_test = null;
              
              
              
                  
                  /**
                   * 不要保存InitialContext于内存中,容易泄露密码!
              
              
              
               
              * 
              
              
               
              * HornetQ User guide:
              
              
               
              *    
               Please note that JMS connections, sessions, producers and consumers are designed to be re-used.
              
              
               
              *    
               It's an anti-pattern to create new connections, sessions, producers and consumers for each message you produce or consume. 
              
              
               
              *    
              
               If you do this, your application will perform very poorly. 
              
              
               
              *    
               尽量重用JMS connections, sessions, producers and consumers。不要每次收发消息都重新创建。
              
              
              
                   */
                  private void initJmsResource(String serverIP, String username, String password)throws Exception {
              
              
              
                  
              Properties props = new Properties();
              
              
                  
              props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
              
              
                 
               //参见:https://community.jboss.org/message/729801#729801
              
              
                  
              props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
              
              
                  
              props.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, "remote://"+serverIP+":4447"));
              
              
                  
              props.put(Context.SECURITY_PRINCIPAL, username);
              
              
                  
              props.put(Context.SECURITY_CREDENTIALS, password);
              
              
                  
              props.put("jboss.naming.client.connect.options.org.xnio.Options.SSL_STARTTLS", "true");
              
              
                  
              props.put("jboss.naming.client.remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "true");
              
              
                  
              //
              
              
                  
              InitialContext jmsInitialContext = new InitialContext(props);
              
              
                 
               
              
              
                  
              jmsConnectionFactory = (ConnectionFactory) jmsInitialContext.lookup("jms/RemoteConnectionFactory");
              
              
                 
               //jmsConnection = connectionFactory.createConnection();//"javax.jms.JMSSecurityException: Unable to validate user: null"
              
              
                 
               jmsConnection = jmsConnectionFactory.createConnection(username,password);
              
              
                  
              jmsSession = jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
              
              
                  
              //
              
              
                  
              //jms/topic/nmsSOE
              
              
                  
              {
              
              
                 
              
               jmsDestination_topic_nmsSOE = (Destination) jmsInitialContext.lookup("jms/topic/nmsSOE");
              
              
                      
              jmsConsumer_topic_nmsSOE = jmsSession.createConsumer(jmsDestination_topic_nmsSOE);
              
              
                  
              }
              
              
                  
              //jms/queue/test
              
              
                  
              {
              
              
                 
              
               jmsDestination_queue_test = (Destination) jmsInitialContext.lookup("jms/queue/test");
              
              
                 
              
               jmsConsumer_queue_test = jmsSession.createConsumer(jmsDestination_queue_test);
              
              
                 
              
               jmsProducer_queue_test = jmsSession.createProducer(jmsDestination_queue_test);
              
              
                  
              }
              
              
                  
              //
              
              
                  
              jmsConnection.start();
              
              
                  
              //
              
              
                  
              testJmsQueue();
              
              
                  
              //
              
              
                  
              initSoeJmsListener();
              
              
              
                  }
                  
                  private void clearJmsResource(){
              
              
              
                 
               //jms/queue/test
              
              
                 
               try{if(jmsConsumer_queue_test!=null){jmsConsumer_queue_test.close();}}catch(Exception e){}
              
              
                 
               try{if(jmsProducer_queue_test!=null){jmsProducer_queue_test.close();}}catch(Exception e){}
              
              
                 
               //jms/topic/nmsSOE
              
              
                 
               try{if(jmsConsumer_topic_nmsSOE!=null){jmsConsumer_topic_nmsSOE.close();}}catch(Exception e){}
              
              
                 
               //global resource
              
              
                 
               try{if(jmsSession!=null){jmsSession.close();}}catch(Exception e){}
              
              
                 
               try{if(jmsConnection!=null){jmsConnection.close();}}catch(Exception e){}
              
              
              
                  }
              
                  
              
              
              
              
              /**
              
              
               
              * https://community.jboss.org/message/721270
              
              
               
              * Like everything else in JBoss AS 7.1.0.Final, JMS is secured by default.  
              
              
               
              * It uses the same security domain as JNDI so you can use the same username and password (i.e. appuser2 and passw0rd respectively) 
              
              
               
              *    
               in your call to javax.jms.ConnectionFactory.createConnection(String, String). 
              
              
               
              */
              
              
              
              private void testJmsQueue(){
              
              
                  
              try {
              
              
                 
              
               TextMessage message = null;
              
              
                      
              int count = 1;
              
              
                      
              String content = "Hellow World from client!";
              
              
                      
              log.info("Sending " + count + " messages to [jms/queue/test] with content: " + content);
              
              
              
              
              
              
              
                      
              //发送测试
              
              
                      
              for (int i = 0; i < count; i++) {
              
              
                          
              message = jmsSession.createTextMessage(content);
              
              
                          
              jmsProducer_queue_test.send(message);
              
              
                      
              }
              
              
                      
              //接收测试
              
              
                      
              for (int i = 0; i < count; i++) {
              
              
                          
              message = (TextMessage) jmsConsumer_queue_test.receive(1000);
              
              
                          
              if(message==null){
              
              
                         
              
               log.warn("1秒之内没有收到消息,该队列[jms/queue/test]中的消息可能已经被MDB接收!");
              
              
                          
              }else{
              
              
                         
              
               log.info("Received message with content " + message.getText());
              
              
                          
              }
              
              
                      
              }
              
              
                  
              } catch (Exception e) {
              
              
                      
              log.error(e);
              
              
                  
              }
              
              
              
              }
              
              
              
              
                  
                  /******************************************************************************************************************************
                   *******************************************************[SOE event]************************************************************
                   ******************************************************************************************************************************/    
                  public static final String ATTR_SOE_NOTIFICATION = "ATTR_SOE_NOTIFICATION";
              
              
              
              
              private void initSoeJmsListener(){
              
              
                 
               try {
              
              
                 
              
               jmsConsumer_topic_nmsSOE.setMessageListener(new MessageListener(){
              
              
                 
              
              
               @Override
              
              
                 
              
              
               public void onMessage(Message msg) {
              
              
                         
              
               try{
              
              
                         
              
              
               if(msg==null){
              
              
                         
              
              
              
               //log.warn("没有收到SOE消息...");//不可能
              
              
                         
              
              
               }else if(msg instanceof ObjectMessage){
              
              
                                      
              ObjectMessage objectMessage = (ObjectMessage) msg;
              
              
                                      
              fireSoeListeners(objectMessage); 
              
              
                                      
              //log.warn("Global SOE JMS 消息 Ne["+objectMessage.getStringProperty(Afn0E.SOE_STRING_PROPERTY_KEY_SOCKETDATA_ADDRESS_HEX)+"]:"+objectMessage);
              
              
                         
              
              
               }else{
              
              
                         
              
              
              
               log.warn("SOE JMS 消息应该是ObjectMessage类型!");
              
              
                         
              
              
               }
              
              
                         
              
               }catch(Exception exp){
              
              
                         
              
              
               log.error(exp);
              
              
                         
              
               }
              
              
                 
              
              
               }
              
              
                      
              });
              
              
                  
              } catch (Exception e) {
              
              
                      
              log.error(e);
              
              
                  
              }
              
              
              
              }
              
              
              
                  private void fireSoeListeners(ObjectMessage objectMessage){
              
              
              
                 
               firePropertyChange(ATTR_SOE_NOTIFICATION,null,objectMessage);
              
              
              
                  }
              }
              
              • 4. Re: JMS Queue Lookup
                xiang yingbing Master

                I am writing an article serial about how to use jboss as 7:

                https://community.jboss.org/wiki/JavaEEApplicationDevelopmentBasedOnJBossAs720-EnglishVersion

                It will be finished in this week, I hope it is useful to you. (Java EE application development based on JBoss as 7.2.0 - 12.How to use JMS)

                Please keep watching on this link, I will update it soon.

                 

                 

                I summarized many configurations and usages about jboss as 7 in my personal site: http://javaarm.com/faces/list.xhtml?mid=42.

                If you understand Chinese, it will be useful to you.

                Even If you do NOT understand Chinese, You can ask questions here http://javaarm.com/faces/list.xhtml?mid=42, I will try my best to answer any of your questions.

                • 5. Re: JMS Queue Lookup
                  xiang yingbing Master

                  I really dislike jboss forum's rick text editor! It does NOT process java code snippet well!