title: java websphere MQ
body: <p>My aim is  to put n number of messages in a for loop to a WebSphere MQ queue using WebSphere MQ java programming.</p>
  
  <p>My java program will run as a standalone program.</p>
  
  <p>If any exception in between , I need to rollback all the messages.</p>
  
  <p>If no exception then I should commit all the messages . </p>
  
  <p>The outside world should not see my messages in the queue until I complete fully. 
  How do I achieve this?</p>
  
  <p><em><strong>Updated with sample code as per reply from T.Rob:</em></strong></p>
  
  <p>Please check if sample code is fine ?</p>
  
  <p>Does setting MQGMO_SYNCPOINT  is only related to my program's invocation ?
  (because similar programs running parallely will also be putting  messages on the same queue and those messages should not gett affected by my program's SYNCPOINT.)</p>
  
  <pre><code>public void sendMsg() {
          MQQueue queue = null;
          MQQueueManager queueManager = null;
          MQMessage mqMessage = null;
          MQPutMessageOptions pmo = null;
          System.out.println("Entering..");
          try {
              MQEnvironment.hostname = "x.x.x.x";
              MQEnvironment.channel = "xxx.SVRCONN";
              MQEnvironment.port = 9999;
  
  
              queueManager = new MQQueueManager("XXXQMANAGER");
              int openOptions = MQConstants.MQOO_OUTPUT;      
              queue = queueManager.accessQueue("XXX_QUEUENAME", openOptions, null, null, null);
  
              pmo = new MQPutMessageOptions(); 
              pmo.options = CMQC.MQGMO_SYNCPOINT;
  
  
              String input = "testing";
              System.out.println("sending messages....");
              for (int i = 0; i &lt; 10; i++) {
                  input = input + ": " + i;
                  mqMessage = new MQMessage();
                  mqMessage.writeString(input);
                  System.out.println("Putting message: " + i);
                  queue.put(mqMessage, pmo);
  
              }
              queueManager.commit();
              System.out.println("Exiting..");
  
          } catch (Exception e) {
              e.printStackTrace();
              try {
                  System.out.println("rolling back messages");
                  if (queueManager != null)
                      queueManager.backout();
              } catch (MQException e1) {
                  e1.printStackTrace();
              }
          } finally {
              try {
                  if (queue != null)
                      queue.close();
                  if (queueManager != null)
                      queueManager.close();
              } catch (MQException e) {
                  e.printStackTrace();
              }
          }
      }
  </code></pre>