Search code examples
jmstibco-emsems

JMS sending message


I want to publish message for which I am writing a JMS application which will publish messages to Tibco EMS queues. There are two queues one is for normal logging and another for exception logging. Now how to send message to two different queues in JMS. Can anyone help me with this as it is very critical?


Solution

  • The very basic JMS API code to send message to queue is shown below. You need to adjust the connection factory as well queue name as per your environment. Also need to adjust initial context settings.

    void sendMessage() {
        Connection con = null;
        try {
    
            // Get the initial context
            Hashtable<String, String> hTable = new Hashtable<String, String>();
            hTable.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
            hTable.put(Context.PROVIDER_URL, "t3://localhost:7001");
            Context ctx = new InitialContext(hTable);
    
            // Create ConnectionFactory
            ConnectionFactory cf = (ConnectionFactory) ctx.lookup("JMS-JNDI-ConFactory");
    
            // Create connection
            con = cf.createConnection();
    
            // Create Non transacted Session with auto ack
            Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
    
            // Create the destination
            Queue queue = (Queue) ctx.lookup("JMS-JNDI-Queue");
    
            // Create MessageProducer for the destination
            MessageProducer producer = session.createProducer(queue);
    
            // Create empty Message with header and properties only
            TextMessage message = session.createTextMessage();
    
            // set the message body
            message.setText("Message-1");
    
            // Send the message
            producer.send(message);
    
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    }