/***************************************************************************/ /* */ /* (c) Copyright IBM Corp. 2002 All rights reserved. */ /* */ /* This sample program is owned by International Business Machines */ /* Corporation or one of its subsidiaries ("IBM") and is copyrighted */ /* and licensed, not sold. */ /* */ /* You may copy, modify, and distribute this sample program in any */ /* form without payment to IBM, for any purpose including developing, */ /* using, marketing or distributing programs that include or are */ /* derivative works of the sample program. */ /* */ /* The sample program is provided to you on an "AS IS" basis, without */ /* warranty of any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, */ /* EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */ /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ /* Some jurisdictions do not allow for the exclusion or limitation of */ /* implied warranties, so the above limitations or exclusions may not */ /* apply to you. IBM shall not be liable for any damages you suffer as */ /* a result of using, modifying or distributing the sample program or */ /* its derivatives. */ /* */ /***************************************************************************/ /* */ /* Program name: StartProcessJMS */ /* */ /* Description: Sample java program that shows creating and starting a */ /* MQSeries Workflow process, building an XML message */ /* that is sent to the MQ queue using the MQ JMS interface */ /* (with NO JNDI). */ /* */ /***************************************************************************/ /* */ /* Function: */ /* */ /* This program accepts 3 parameters; a first name, last name and an */ /* optional -c to indicate this is a client transport application. */ /* If -c is not specified, then server bindings are used. The last name */ /* is used to name the process instance. */ /* */ /* This program is run as follows: */ /* */ /* java -Dc=hostname StartProcessJMS firstname lastname */ /* */ /* If no parameters are specified, an error is displayed */ /* You must specify a first name and last name */ /* The -c is optional and, if specified, must be followed by your */ /* system hostname. If you have the wrong hostname, you will receive */ /* an MQ reason code of 2059. */ /* */ /* This sample program will create and start an instance of the */ /* CreditRequest sample process shipped with MQSeries Workflow (fmccred.fdl)*/ /* */ /* Factory objects are created at runtime (nojndi interface) */ /* */ /* */ /* XML messages for MQSeries Workflow are put into the queue EXEXMLINPUTQ */ /* The queue manager is set to FMCQM in this code. (MQWF default) */ /* The channel name is set to FMCQM.CL.TCP (MQWF default) */ /* NOTE: by default the MCAUSER on this channel is set to user fmc */ /* User fmc must exist in the MQWF runtime database. If not, you */ /* will receive RC = 10, unknown userid. */ /* The port is set to 5010 (MQWF default) */ /* */ /* Process name = CreditRequest */ /* InputContainer = PersonInfo */ /* container members = FirstName and LastName both are string types */ /* */ /* The following jar files must be in your CLASSPATH statement */ /* For JDK 1.3 */ /* .;\MQTOP\Java\Lib;\MQTOP\java\lib\com.ibm.mqjms.jar; */ /* \MQTOP\java\lib\jms.jar;\MQTOP\java\lib\com.ibm.mq.jar; */ /* \MQTOP\java\lib\connector.jar;\MQTOP\java\lib\jta.jar */ /* */ /* For JDK 1.2.2 */ /* same as JDK1.3 plus \MQTOP\Java\Lib\jndi.jar */ /* */ /* You will need to compile this using the command: */ /* javac StartProcessJMS.java */ /* */ /* To run it, make sure the MQSeries Workflow server is started. */ /* Make sure you have imported and translated fmccred.fdl */ /* If using client transport, then must have user fmc in MQWF RT DB */ /* Type: java StartProcessJMS firstname lastname */ /* */ /* This program has been tested with: */ /* MQSeries V5.2 CSD 2 */ /* JDK 1.2.2 (and 1.3) */ /* DB2 V7.2 */ /* Windows 2000 */ /* MQSeries Workflow 3.3.2 */ /***************************************************************************/ // Import of Sun's JMS interface import javax.jms.*; // The following import would not normally be required in a JMS application, // but is included here to allow us to bypass the JNDI lookup stage import com.ibm.mq.jms.*; public class StartProcessJMS { // These are used when the -nojndi flag is given. public static String QMGR = "FMCQM"; public static final String QUEUE = "EXEXMLINPUTQ" ; public static void main( String[] args ) { String outString = null; Queue ioQueue = null; Queue replytoqu = null; QueueSession session = null; QueueConnection connection = null; QueueConnectionFactory factory = null; String firstname = null; String lastname = null; String hostname = null; boolean isClient =false; //parse the incoming arguments int argnum = args.length; if ((argnum !=2 )) { System.out.println("You must specify at least a first and last name." ); System.out.println("Format is: java -Dc=hostname StartProcessJMS firstname lastname"); System.out.println("Where -Dc=hostname is an optional parameter."); System.exit(-1); } firstname=args[0]; lastname=args[1]; System.out.println("Firstname = "+firstname); System.out.println("Lastname = "+lastname); hostname=System.getProperty("c"); System.out.println("hostname="+hostname); if (hostname!=null) isClient=true; try { // create a connection factory directly // Note that this requires a reference to the underlying // class that implements QueueConnectionFactory System.out.println("Creating a QueueConnectionFactory"); factory = new MQQueueConnectionFactory(); ((MQQueueConnectionFactory)factory).setQueueManager(QMGR); if (isClient) { // Set transport to CLIENT mode // NOTE: if you have an MCAUSER specified on the channel, then that user must // exist in the MQWF runtime database. If not, you will get unknown userid System.out.println("Transport type is set to CLIENT."); ((MQQueueConnectionFactory)factory).setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP); ((MQQueueConnectionFactory)factory).setHostName(hostname); ((MQQueueConnectionFactory)factory).setChannel("FMCQM.CL.TCP"); ((MQQueueConnectionFactory)factory).setPort(5010); } // Create a QueueConnection from the QueueConnectionFactory System.out.println("Creating a Connection"); connection = factory.createQueueConnection(); // Start the connection System.out.println("Starting the Connection"); connection.start(); // We now create a QueueSession from the connection. Here we // specify that it shouldn't be transacted, and that it should // automatically acknowledge received messages System.out.println("Creating a Session"); boolean transacted = false; session = connection.createQueueSession( transacted, Session.AUTO_ACKNOWLEDGE); // Use the session to create the queue object, supplying // the required MQSeries-specific parameter ioQueue = session.createQueue( QUEUE ); // The MQWF execution server does not like JMS messages (RFH2) prior to MQWF 3.3.2 SP2 // If you have SP2 applied, then you should not need to use this statement ((MQQueue)ioQueue).setTargetClient(1); // We now use the session to create a QueueSender, passing in the // destination (the Queue object) as a parameter System.out.println("Creating a QueueSender"); QueueSender queueSender = session.createSender(ioQueue); // The session is used to create messages, so create an empty // TextMessage and fill it with some data System.out.println( "Creating a TextMessage" ); TextMessage outMessage = session.createTextMessage(); System.out.println("Adding Text"); // debugging for processing of mqwf xml api response // look for the RC in the reply to queue message using amqsbcg // if there are no messages in the queue below, then it was successful replytoqu = session.createQueue("SYSTEM.DEFAULT.LOCAL.QUEUE"); outMessage.setJMSReplyTo(replytoqu); // ------------------------------------------------- // Build the XML message that will start the process // ------------------------------------------------- String procTempName="CreditRequest"; String procInstName = lastname; String contName = "PersonInfo"; outString= "" + "\n " + "\n " + "\n No" + "\n " + "\n " + "\n " + procTempName + "" + "\n " + procInstName + "" + "\n " + "\n <" + contName + ">" + "\n " + firstname + "" + "" + lastname + "" + ""+ "\n " + "\n " + "\n \n\n\n"; outMessage.setText(outString); // Ask the QueueSender to send the message we have created System.out.println( "Sending the message to " + ioQueue.getQueueName() ); queueSender.send(outMessage); System.out.println("Closing QueueSender"); queueSender.close(); // Closing QueueSesssion. System.out.println("Closing Session"); session.close(); session = null; // Closing QueueConnection. System.out.println("Closing Connection"); connection.close(); connection = null; } catch ( JMSException je ) { System.out.println("caught JMSException: " + je); // check for a linked exception that provides more detail Exception le = je.getLinkedException(); if (le != null) System.out.println("linked exception: "+le); } catch ( Exception e ) { // This catches any exception thrown in the main body of // the program, displaying it on screen System.out.println("Caught exception: " + e ); } finally { // A finally block is a good place to ensure that we don't forget // to close the most important JMS objects try { if (session != null) { System.out.println("closing session"); session.close(); } if (connection != null) { System.out.println("closing connection"); connection.close(); } } catch (JMSException je) { System.out.println("failed with "+je); } } System.out.println("finished"); } }