JMSList.java :  » ESB » cbesb-1.2 » com » bostechcorp » cbesb » runtime » jms » Java Open Source

Java Open Source » ESB » cbesb 1.2 
cbesb 1.2 » com » bostechcorp » cbesb » runtime » jms » JMSList.java
/*
 * ChainBuilder ESB
 *          Visual Enterprise Integration
 * 
 * Copyright (C) 2006 Bostech Corporation
 * 
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the 
 * Free Software Foundation; either version 2 of the License, or (at your option) 
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *JMSList.java
 *LPS
 *Dec 12, 2007
 */
package com.bostechcorp.cbesb.runtime.jms;

import java.util.Hashtable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.directory.InitialDirContext;

/**
 * @author LPS
 *
 */
public class JMSList {

  public void runTest1() {
    // connect to JNDI service provider
    InitialDirContext jndiContext = null;
    Hashtable<String, String> environment = new Hashtable<String, String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
    environment.put(Context.PROVIDER_URL, "tcp://localhost:61616");
    environment.put(Context.REFERRAL, "throw");
    try {
      jndiContext = new InitialDirContext(environment);
      
      ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("ConnectionFactory");
            Destination sendDest = (Destination) jndiContext.lookup("dynamicQueues/testqueue1_jms");
            Destination recvDest = (Destination) jndiContext.lookup("dynamicQueues/testreply1_jms");

      System.out.println("destination="+sendDest);

      
            Connection connection = connectionFactory.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer producer = session.createProducer(sendDest);

            String sendString = "\u0434\u043e\u0431\u0440";
            System.out.println("sending:\n"+dumpStringAsUnicode(sendString));
            TextMessage outMsg = session.createTextMessage(sendString);
            outMsg.setJMSReplyTo(recvDest);
            System.out.println("before send messageID="+outMsg.getJMSMessageID());
            producer.send(outMsg);
            String sentMessageId = outMsg.getJMSMessageID();
            System.out.println("after send messageID="+sentMessageId);
            
            
            MessageConsumer consumer = session.createConsumer(recvDest);

            System.out.println("receiving...");
            connection.start();
            Message inMessage = consumer.receive();
            String inCorrelationId = inMessage.getJMSCorrelationID();
            System.out.println("got message correlationID="+inCorrelationId);
            
            for (int i=0; i<inCorrelationId.length(); i++) {
              if (inCorrelationId.charAt(i) != sentMessageId.charAt(i)) {
                System.out.println("****ERROR, Received correlation-id does not match sent message-id");
                break;
              }
            }
            
            TextMessage inTextMessage = null;
            if (inMessage instanceof TextMessage) {
              inTextMessage = (TextMessage)inMessage;
            } else {
              System.err.println("error - received a non-text message: "+inMessage);
            }
            System.out.println("got:\n"+dumpStringAsUnicode(inTextMessage.getText()));
            
            
            connection.close();
      
    } catch (Exception e) {
      e.printStackTrace();
      System.err.println("exception: "+e);
    }
  }
  
  public void list()
  {
    InitialDirContext jndiContext = null;
    Hashtable<String, String> environment = new Hashtable<String, String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
    environment.put(Context.PROVIDER_URL, "tcp://localhost:61616");
    environment.put(Context.REFERRAL, "throw");
    
    
  }
  
  public static String dumpStringAsUnicode(String s) {
    int charsPerLine= 16;
    StringBuffer out = new StringBuffer();
    StringBuffer left = null;
    StringBuffer right = null;
    int position=0;
    for (char ch : s.toCharArray()) {
      if (left == null) left = new StringBuffer();
      if (right == null) right = new StringBuffer();
      left.append((ch < 32) ? '.' : ch);
      right.append(String.format("%4X ", (int)ch));
      if (++position == charsPerLine) {
        position=0;
        out.append("  "+left+" - "+right+"\n");
        left = null;
        right = null;
      }
    }
    if (left != null) {
      while (left.length() < charsPerLine) left.append(" ");
      out.append("  "+left+" - "+right+"\n");
    }
    return new String(out);
  }
  
  public static void main(String[] args) 
  {
    JMSList lk = new JMSList();
    lk.runTest1();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.