Websphere MQ File Transfer Java/JAXB file transfer

Prerequisites for the Java JAB Setup:

1. Collect the XSD files that are shipped with WebSphere MQ File Transfer Edition

2. Use the xjc utility to create the Java classes that allow manipulating the XML documents that conform to those XSD files

3. Create a WebSphere MQ text message containing the WebSphere MQ File Transfer Edition request

4. Put that message to the appropriate WebSphere MQ queue.

Step 1:

Create the Java classes that allow WebSphere MQ File Transfer Edition schema file manipulation. Go to the directory E:\student\Java. The sub directory xsd contains the schema files while the sub directory jaxb-ri-20070122 contains the JAXB support. This support has been already installed here, but it can be downloaded from https://jaxb.dev.java.net/2.0.5/

The sub directory temp is empty at the moment.

Open a DOS command prompt

Navigate to E:\student\Java\jaxb-ri-20070122\bin.

Enter this command:

xjc -d E:\student\Java\temp -p generated E:\student\Java\xsd\fteutils.xsd

Step 2: Now we create the main file transfer request classes via the command:

xjc -d E:\student\Java\temp -p generated E:\student\Java\xsd\FileTransfer.xsd

All the generated classes are now in the directory E:\student\Java\temp\generated

Step 3: Open Java perspective in WMQ Explorer

Create a new Java Program

Step 4: Import external Jars into project:

The Java program will use external jar files:

– the JAXB jar file to support JAXB

– the WebSphere MQ jar files to support WebSphere MQ programming

For this reason we have to set references of the Java project to the appropriate jar files

Right click on the Java project à Properties

Add the following packages:

com.ibm.jaxws.thinclient_6.1.0.jar

com.ibm.mq.jar

com.ibm.mq.jmqi.jar

com.ibm.mq.jms.Nojndi.jar

Step 5: Create two new packages

1. generated (it will contain the Java classes generated by JAXB)

2. it.mqfte.xml (it will contain the developed program

package it.mqfte.xml;

import generated.AgentClientType;

import generated.AgentType;

import generated.CheckSumMethod;

import generated.DestinationType;

import generated.ExistType;

import generated.FileDestinationType;

import generated.FileSourceType;

import generated.FileType;

import generated.ItemType;

import generated.ManagedTransferType;

import generated.ModeType;

import generated.ObjectFactory;

import generated.OrigRequestType;

import generated.Request;

import generated.SourceDispositionType;

import generated.TransferSetType;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.Marshaller;

import com.ibm.mq.constants.*;

import com.ibm.mq.MQEnvironment;

import com.ibm.mq.MQException;

import com.ibm.mq.MQMessage;

import com.ibm.mq.MQPutMessageOptions;

import com.ibm.mq.MQQueue;

import com.ibm.mq.MQQueueManager;

public class QueueTransfer {

/**

* @param args

*/

public static void main(String[] args) {

/* We will create an XML command as shown below.

* Once created it will be put to the queue SYSTEM.FTE.COMMAND.DOPEY

*

* <?xml version=”1.0″ encoding=”UTF-8″?>

* <request version=”1.00″

*        xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;

*        xsi:noNamespaceSchemaLocation=”FileTransfer.xsd”>

*          <managedTransfer>

*              <originator>

*                    <hostName>win2k3base</hostName>

*                    <userID>esbuser</userID>

*              </originator>

*              <sourceAgent agent=”AGT1WIN”

*                QMgr=”QM_AGENTHOST1″/>

*              <destinationAgent agent=”AGT1WIN”

*              QMgr=”QM_AGENTHOST2″/>

*                <transferSet>

*                    <item mode=”binary” checksumMethod=”MD5″>

*                          <source recursive=”false” disposition=”leave”>

*                                <file>E:\student\BinaryFileLarge.zip</file>

*                          </source>

*                          <destination type=”file” exist=”error”>

*                               <file>C:\FTEJava.zip</file>

*                          </destination>

*                    </item>

*              </transferSet>

*        </managedTransfer>

*  </request>

*/

try {

// Create the JAX environment.

// Create a marshaller. A marshaller is responsible of building the XML wire format

// then create a factory. A factory is capable to build the XML tree in memory.

JAXBContext jaxbContext = JAXBContext.newInstance(“generated”);

Marshaller marshaller = jaxbContext.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

//marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, “FileTransfer.xsd”);

ObjectFactory factory=new ObjectFactory();

// Create the object request which is the outermost complex type

Request request=(Request)(factory.createRequest());

request.setVersion(“1.00”);

// Create now a managed file transfer type. It is the first (and only) child of request

// mt is initially empty

ManagedTransferType mt = (ManagedTransferType)(factory.createManagedTransferType());

// Create the originator request.

// It is the 1st child of mt and it holds source agent address and user id

// then set the originator as part of mt

OrigRequestType oi = (OrigRequestType)(factory.createOrigRequestType());

// ==> set host name and user id of the agent we will send the request to (DOPEY)

oi.setHostName(“win2k3base”);

oi.setUserID(“esbuser”);

mt.setOriginator(oi);

// Create the source agent. It has a name and queue manager name

// then set source agent as part of mt

AgentType sa  = (AgentType)(factory.createAgentType());

// ==> set source agent name and source queue manager name

sa.setAgent(“AGT1WIN”);

sa.setQMgr(“QM_AGENTHOST1”);

mt.setSourceAgent(sa);

// Create the destination agent. It has a name and queue manager name

// then set destination agent as part of mt

AgentClientType da  = (AgentClientType)(factory.createAgentClientType());

// ==> set destination agent name and destination queue manager name

da.setAgent(“AGT1WIN”);

da.setQMgr(“QM_AGENTHOST1”);

mt.setDestinationAgent(da);

// Now create the transfer set. Apart from the priority it is initially empty

TransferSetType ts  = (TransferSetType)(factory.createTransferSetType());

ts.setPriority(“0”);

// Create one item. We only have one item.

// Set its mode to binary and turn on check sum

ItemType it  = (ItemType)(factory.createItemType());

it.setMode(ModeType.BINARY);

it.setChecksumMethod(CheckSumMethod.MD_5);

// Create the source information and put the file name in it

// remember to use double back slashes

FileSourceType fs  = (FileSourceType)(factory.createFileSourceType());

fs.setDisposition(SourceDispositionType.LEAVE);

fs.setRecursive(false);

FileType ft  = (FileType)(factory.createFileType());

// ==> set source file name. This is a windows file name. Use double back slash \\

//     to represent back slash in file name

ft.setValue(“E:\\student\\BinaryFileLarge.zip”);

fs.setFile(ft);

it.setSource(fs);

// Create the destination file and store it into the item

FileDestinationType fd  = (FileDestinationType)(factory.createFileDestinationType());

fd.setExist(ExistType.OVERWRITE);

fd.setType(DestinationType.FILE);

FileType ftd  = (FileType)(factory.createFileType());

// ==> set destination file name

ftd.setValue(“C:\\BinaryFileLarge.zip”);

fd.setFile(ftd);

it.setDestination(fd);

// Add the item to the transfer set

ts.getItem().add(it);

mt.setTransferSet(ts);

// Smash the manager file transfer into the request

request.setManagedTransfer(mt);

// Marshal the request to a working byte array

// Finally convert the byte array into a string

ByteArrayOutputStream baos = new ByteArrayOutputStream();

marshaller.marshal(request, baos);

String st = new String(baos.toString());

String s1 = “version=\”1.00\””;

String s2 = “version=\”1.00\” ” +

“xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\” ” +

“xsi:noNamespaceSchemaLocation=\”FileTransfer.xsd\””;

String stResult= st.replaceFirst(s1, s2);

System.out.println(stResult);

//*** Now put the request to the target queue

writeQ(“QM_AGENTHOST1”,

“win2k3base”,

“SYSTEM.DEF.SVRCONN”,

1415,

“SYSTEM.FTE.COMMAND.AGT1WIN”,

stResult

);

} catch (Exception e) {

e.printStackTrace();

}

}

/***********************************************************************************************

*  Connect to the queue manager and put the ft message to the target queue

*  Input parameters: queue manager name

*                    host name

*                    channel name

*                    port number

*                    queue name

*                    command string

*  Output parameter: none    *

*/

public static void writeQ(String qManager, String host, String chl, int port, String queue, String cmd) {

MQEnvironment.hostname = host;

MQEnvironment.channel = chl;

MQEnvironment.port = port;

//(MQEnvironment.properties).put(MQConstants.TRANSPORT_PROPERTY  , MQConstants.TRANSPORT_MQSERIES_CLIENT);

try {

MQQueueManager qMgr = new MQQueueManager(qManager);

int openOptions = MQConstants.MQOO_OUTPUT;

MQQueue myQueue = qMgr.accessQueue(queue, openOptions);

MQMessage hello_world = new MQMessage();

hello_world.writeString(cmd);

MQPutMessageOptions pmo = new MQPutMessageOptions();

myQueue.put(hello_world,pmo);

System.out.println(“Command has been queued”);

myQueue.close();

qMgr.disconnect();

}

catch (MQException ex){

System.out.println(

“An MQ error occurred : Completion code ” +

+ ex.completionCode

+ ” Reason code ”

+ ex.reasonCode);

}

catch (IOException ex) {

System.out.println(“Io exception: ” + ex);

}

}

}

Final MQ FTE Definition File Created:

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
<request version=”1.00″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=”FileTransfer.xsd”>
<managedTransfer>
<originator>
<hostName>win2k3base</hostName>
<userID>esbuser</userID>
</originator>
<sourceAgent agent=”AGT1WIN” QMgr=”QM_AGENTHOST1″/>
<destinationAgent agent=”AGT1WIN” QMgr=”QM_AGENTHOST1″/>
<transferSet priority=”0″>
<item mode=”binary” checksumMethod=”MD5″>
<source recursive=”false” disposition=”leave”>
<file>E:\student\BinaryFileLarge.zip</file>
</source>
<destination type=”file” exist=”overwrite”>
<file>C:\BinaryFileLarge.zip</file>
</destination>
</item>
</transferSet>
</managedTransfer>
</request>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: