JMS connection for send message to mq queue using thread pool



Step 0>


D:\Software\Softwares\MQ7.0.1.5_cdimage_trial\cdimage_trial


  1. Install MQSeries using Softwares\MQ7.0.1.3_cdimage_trial\cdimage_trial\Setup.exe.
  2. Click WebSphere MQ Installation button. You might receive an error that WebSphere Eclipse Platform Version 3.0.1 is not installed.
  3. Click on the “View Launchpad”. Click the Software Requirements button.
  4. Expand WebSphere Eclipse Platform Version 3.0.1 and click on Network PB.  Go to the folder < Softwares\MQ7.0.1.3_cdimage_trial> \cdimage_trial\Prereqs\IES  and click on setup.exe file will be already entered so click the Open PB. The installation will begin.
  5. Click OK PB for the language. Default is English.
  6. Click Next PB on the welcome screen for WebSphere Eclipse Platform.
  7. Select I accept the terms in the license agreement RB, click Next PB.
  8. Click Next PB for the destination Folder.
  9. Click the Install PB to begin the installation of WebSphere Eclipse platform.
  10. Click the Finish PB and now click Refresh button. WebSphere Eclipse Platform Version 3.0.1 is installed.
  11. Click WebSphere MQ Installation button.
  12. Click Launch WebSphere MQ  Installer button.
  13. Welcome window click Next PB.
  14. Select I accept the terms in the license agreement RB, click Next PB.
  15. Select Custom RB and click Next PB.
  16. Click Change PB, enter C:\MQSeries for folder name, click OK PB.
  17. Click Next PB.
  18. Click Next PB, when it indicates data-files folder, leave as C:\MQSeries.
  19. Click Next PB, when it indicates log files folder, C:\MQSeries\log.
  20. Select all 4 features (Server, Client, Java Messaging, and Development Toolkit) on Features window.
  21. Click Next PB.
  22. Click Install PB.
  23. Answer ‘Yes’ to license query about purchasing sufficient license units.
  24. Click the Finish PB when file copying completes.
  25. Click Next PB when Prepare WebSphere MQ Wizard window appears. WebSphere MQ begins setting itself up. Make sure the NO RB is selected for the question - Are any of the domain controllers in your network running Windows 2000 Server and then click NEXT PB.
  26. Click the Setup the Default Configuration link to start the Default Configuration Wizard.
  27. Click Next PB.
  28. Click Next PB.
  29. uncheck box Join the queue manager to the default cluster called, and click Next PB
  30. Click Finish PB, Default configuration begins. Ignore ‘default configuration is partly complete’ warning message. Click Close PB when done.
  31. Click Next PB in Prepare WebSphere MQ Wizard window.
  32. When Wizard is finished, uncheck Launch WebSphere MQ Getting Started Help  and uncheck Launch Notepad to view the release notes and click Finish PB.
  33. Some users may receive the following error: MQSeries is not properly configured.  This error is caused because your NA\Login needs to be added to the mqm group. If you get this error, Click Cancel PB and follow steps 20-25.
  34. Click Start>Settings>Control Panel>Administrative Tools>Computer Management.
  35. Expand Local Users and Groups in left pane. Then Select Groups.
  36. In Right Pane, double click on mqm. This opens the properties window for the mqm group.
  37. Click Add PB, select na.uis.unisys.com from Look in: drop down list.
  38. In bottom pane enter your NA Domain login and click Check Names PB. Your name will appear underlined. Click OK PB. Now your NA Domain login appears as a user of the mqm group.
  39. Click Apply PB, then close out..
  40. Installation is complete.


1.1.      UnInstalling WebSphere MQ 7.0.1.7



  1. Go to control panel
  2. Select programs and features.
  3. Select IBM webservice MQ
  4. Uninstall
  5. If need to reboot do it
  6. Else if any warning is there just proceded.
  7. Thanks
After than add
C:\MQSeries\java\lib and pest all jar file in build path


Step 1>  
/**
 *
 */
package com.demo;
import java.util.concurrent.ExecutionException;



/**
 * @author MandalKC
 *
 */
public class JmsMain {
 static JmsCallMethod jmsCall;
 /**
  * @param args
  * @throws ExecutionException
  * @throws InterruptedException
  */
 public static void main(String[] args) throws InterruptedException, ExecutionException {

  long startTime = System.currentTimeMillis();
    jmsCall=new JmsCallMethod();
   jmsCall.threadCreation();
  long endTime   = System.currentTimeMillis();
  long totalTime = endTime - startTime;
  System.out.println(totalTime);
  //jmsCall.browse();
  //jmsCall.cleanUp();
  // jmsCall.cleanUpAllQueue();
  //jmsCall.receiveMessage();
 // jmsCall.transferData();

 }
}




Step 2>


package com.demo;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import javax.jms.JMSException;
public class JmsCallMethod  {
 QueueInterfaceImpl qmimpl=new QueueInterfaceImpl();

 // This is send message to a particular queue
 void sendMessage(){
 String qname="csanfmstatus";
 String qManager="QM_IN_MANDALKC_1";
 final String text = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"+
                                "<Emp id=\"1\"><name>Kartik</name><age>30</age>\n"+
                                "<role>Developer</role><gen>Male</gen></Emp><?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"+
                                "<Emp id=\"1\"><name>Kartik</name><age>30</age>\n";
 //String text="Kartik is a good boy";
 try {
  qmimpl.sendMessage(qManager,qname,text);
 } catch (JMSException e) {
  e.printStackTrace();
 }
 }

  void threadCreation() throws InterruptedException, ExecutionException{
   Future taskTwo = null;
   Future taskThree = null;
   ExecutorService executor = Executors.newFixedThreadPool(20);
      for(int i = 0; i < 20; i++) {
     
     executor.execute(new MyThread("TaskTwo"+i,i));

      }
      executor.shutdown();
      System.out.println("-----------------------");
      // wait until all tasks are finished
      executor.awaitTermination(1, TimeUnit.SECONDS);
      System.out.println("All tasks are finished!");

  }
}


Step 3>


package com.demo;
public class MyThread implements Runnable {
 JmsCallMethod jmsCall=new JmsCallMethod();
 private String myName;
 private int count;
 //private final long timeSleep;
 MyThread(String name, int newcount) {
  this.myName = name;
     this.count = newcount;
   //  this.timeSleep = newtimeSleep;
 }

 @Override
 public void run() {
  // TODO Auto-generated method stub
  long startTime = System.currentTimeMillis();

  jmsCall.sendMessage();
  long endTime   = System.currentTimeMillis();
  long totalTime = endTime - startTime;
  System.out.println("Thread name ="+this.myName+"and execute time is = "+totalTime);
//     try {
//   Thread.sleep(this.timeSleep);
//  } catch (InterruptedException e) {
//   // TODO Auto-generated catch block
//   e.printStackTrace();
//  }
 }
}




Step 4>


package com.demo;
import java.io.IOException;
import javax.jms.JMSException;
/**
 *
 * @author MandalKC
 *
 */
public interface QueueInterface {

  /**
   *
   * @param queueManagerName queueManagerName
   * @param queueName queueName
   * @param sendTextMessage sendTextMessage
   * @throws JMSException JMSException
   */
  void sendMessage(String queueManagerName,String queueName,String sendTextMessage) throws JMSException;

}


Step 5>


package com.demo;
import java.io.IOException;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.MQQueue;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueReceiver;
import com.ibm.mq.jms.MQQueueSender;
import com.ibm.mq.jms.MQQueueSession;
import com.ibm.msg.client.wmq.WMQConstants;
public class QueueInterfaceImpl implements QueueInterface {
 public static MQQueueConnectionFactory connectionFactory = null;
 public static MQQueueConnection connection = null;
 public static MQQueueSession receiveSession = null;
 public static MQQueueSession senderSession = null;
 public static MQQueueReceiver receiverQueue = null;
 public static MQQueueSession sendSession = null;
 public static MQQueueSender senderQueue =  null;

 //ConnectionMq cq=new ConnectionMq();

 /**
  *
  * @param queueManager queueManager
  * @purpose Queue manager connection implementation here
  */
 public void initialize(String queueManager) {
  try {
    connectionFactory = new MQQueueConnectionFactory();
    if (connectionFactory != null) {
     connectionFactory.setHostName("localhost");
     connectionFactory.setPort(1414);
     connectionFactory.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_BINDINGS);
     connectionFactory.setQueueManager(queueManager);
     connectionFactory.setChannel("SYSTEM.ADMIN.SVRCONN");
    }
  } catch (JMSException e) {
   System.out.println("initialize Error in Configuration Parameters for MQ:"
                            + e.getMessage());
  } catch (Exception e) {
   System.out.println("initialize Error in Configuration Parameters for MQ:"
                    + e.getMessage());
  } catch (Throwable t) {
   System.out.println("initialize Error in Configuration Parameters for MQ:"
                    + t.getMessage());
  }
 }




 @Override
 public void sendMessage(String queueManagerName,String queueName,String sendTextMessage) throws JMSException {
  initialize(queueManagerName);
  connection = (MQQueueConnection) connectionFactory.createQueueConnection();
  try {
   senderSession = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
   com.ibm.mq.jms.MQQueue queue = (com.ibm.mq.jms.MQQueue) senderSession.createQueue(queueName);
      senderQueue=  (MQQueueSender) senderSession.createSender(queue);
      long uniqueNumber = System.currentTimeMillis() % 1000;
      TextMessage message = (TextMessage) senderSession.createTextMessage(sendTextMessage+ uniqueNumber);   
      // Start the connection
      connection.start();
      senderQueue.send(message);
      System.out.println("Sent message to Queue MyTestQueue: " + message.getText());
      senderQueue.close();
     // senderSession.commit();
      senderSession.close();
      connection.close();
  } catch (JMSException e) {
   e.printStackTrace();
  }
    }
}

Previous
Next Post »