0 Replies Latest reply on May 4, 2011 10:42 AM by han chen

    Problem about ActiveMQ send messages

    han chen Newbie

      I had problem using the codes below to send ActiveMQ-JMS messages, every time i send a single message,  a connection will be established  to the ActiveMQ server, but i got many connections hanged.
      I wonder where to put the initialization code to make sure that only one instance of MessageProducer is created so that every call of producer.send(txtMessage) use the same conncetion。
      eager and appreciate for any help!




      public void sendmessage(long id,long keyid,String op,String tab) {
                try {
                              // some initialization codes
                        Context initialContext = new InitialContext();  
                     TopicConnectionFactory connectionFactory1 = (TopicConnectionFactory) initialContext.lookup("java:activemq/TopicConnectionFactory");   
                     Connection con=connectionFactory1.createConnection(); 
                     javax.jms.Session session =  con.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);                     
                     Topic topic = (Topic) initialContext.lookup("activemq/topic/apparatus");   
                     MessageProducer producer = session.createProducer(topic);   
                     producer.setDeliveryMode(DeliveryMode.PERSISTENT);  
                     producer.setTimeToLive(Message.DEFAULT_TIME_TO_LIVE); 
                     
                              //message text     
                              TextMessage txtMessage = session.createTextMessage();
                     Document doc=new Document();
                     Element element = new Element("msg");
                     doc.setRootElement(element);
                     element.addContent(new Element("id").setText(""+id));
                     element.addContent(new Element("tab").setText(tab));
                     element.addContent(new Element("op").setText(op));
                     Element keyElement = new Element("keyid");
                     keyElement.addContent(new Element("keyid").setText(""+keyid));
                     element.addContent(keyElement);
                     XMLOutputter outp=new XMLOutputter();
                     Format format=Format.getPrettyFormat();
                     format.setEncoding("GBK");
                     outp.setFormat(format);
                        String str=outp.outputString(doc);
                     System.out.println(str);
                     txtMessage.setText(str);
                              
                              // Send the message
                     producer.send(txtMessage);
                     System.out.println("success");
                     producer.close();   
                     session.close();   
                     con.close();
                }catch(NamingException e){
                     e.printStackTrace();
                }catch(JMSException e){
                     e.printStackTrace();
                }
           }