|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
sending a file as a message |
« View previous topic :: View next topic » |
Author |
Message
|
joecherian |
Posted: Wed Mar 05, 2003 7:56 pm Post subject: sending a file as a message |
|
|
Novice
Joined: 15 Dec 2002 Posts: 23 Location: India
|
Hello,
can anyone give me some sample code as to how i can send a file as a message to the MQSeries .? I am using base Java to access MQSeries
Thanks
'Joe Cherian |
|
Back to top |
|
 |
RogerLacroix |
Posted: Wed Mar 05, 2003 10:12 pm Post subject: |
|
|
 Jedi Knight
Joined: 15 May 2001 Posts: 3264 Location: London, ON Canada
|
Here's a sample that I wrote for someone else. It's not pretty but it does what you want. Someday I'll clean it up and post it to my site.
later
Roger...
Code: |
// ==================================================================
//
// Program Name
// File2Msg
//
// Last date of modification
// 6 March 2003
//
// Description
// This java class will read a file and send the entire file
// as one message.
//
// Sample Command Line Parameters
// -h 127.0.0.1 -p 1414 -c CLIENT.CHANNEL -m MQA1 -q TEST.QUEUE -f filename
//
// Copyright(C), Roger Lacroix, Capitalware
//
// ------------------------------------------------------------------
import com.ibm.mq.*;
import java.io.IOException;
import java.util.Hashtable;
import java.io.*;
public class File2Msg {
private MQQueueManager _queueManager = null;
private Hashtable params = null;
public int port = 1414;
public String hostname = "127.0.0.1";
public String channel = "CLIENT.TO.MQA1";
public String qManager = "MQA1";
public String outputQName = "SYSTEM.DEFAULT.LOCAL.QUEUE";
public String inputFileName = "in.txt";
public File2Msg()
{
super();
}
private boolean allParamsPresent()
{
boolean b = params.containsKey("-h") &&
params.containsKey("-p") &&
params.containsKey("-c") &&
params.containsKey("-m") &&
params.containsKey("-f") &&
params.containsKey("-q");
if (b)
{
try
{
port = Integer.parseInt((String) params.get("-p"));
}
catch (NumberFormatException e)
{
b = false;
}
// Set up MQ environment
hostname = (String) params.get("-h");
channel = (String) params.get("-c");
qManager = (String) params.get("-m");
outputQName = (String) params.get("-q");
inputFileName = (String) params.get("-f");
}
return b;
}
private void init(String[] args) throws IllegalArgumentException
{
params = new Hashtable(5);
if (args.length > 0 && (args.length % 2) == 0)
{
for (int i = 0; i < args.length; i+=2)
{
params.put(args[i], args[i+1]);
}
}
else
{
throw new IllegalArgumentException();
}
if (allParamsPresent())
{
// Set up MQ environment
MQEnvironment.hostname = hostname;
MQEnvironment.channel = channel;
MQEnvironment.port = port;
}
else
{
throw new IllegalArgumentException();
}
}
public static void main(String[] args)
{
File2Msg write = new File2Msg();
try
{
write.init(args);
write.selectQMgr();
write.write();
}
catch (IllegalArgumentException e)
{
System.out.println("Usage: java File2Msg <-h host> <-p port> <-c channel> <-m QueueManagerName> <-q QueueName> <-f FileName>");
System.exit(1);
}
catch (MQException e)
{
System.out.println(e);
System.exit(1);
}
}
private void selectQMgr() throws MQException
{
_queueManager = new MQQueueManager(qManager);
}
private void write() throws MQException
{
String line;
int lineNum=0;
int openOptions = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING;
try
{
MQQueue queue = _queueManager.accessQueue( outputQName,
openOptions,
null, // default q manager
null, // no dynamic q name
null ); // no alternate user id
System.out.println("File2Msg v1.0 connected");
// Define a simple MQ message, and write some text in UTF format..
MQMessage sendmsg = new MQMessage();
sendmsg.format = MQC.MQFMT_STRING;
sendmsg.feedback = MQC.MQFB_NONE;
sendmsg.messageType = MQC.MQMT_DATAGRAM;
sendmsg.replyToQueueName = "ROGER.QUEUE";
sendmsg.replyToQueueManagerName = qManager;
MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults, same
// as MQPMO_DEFAULT constant
sendmsg.messageId = MQC.MQMI_NONE;
sendmsg.correlationId = MQC.MQCI_NONE;
sendmsg.writeString(ReadFile(inputFileName));
// put the message on the queue
queue.put(sendmsg, pmo);
sendmsg.clearMessage();
queue.close();
_queueManager.disconnect();
}
catch (com.ibm.mq.MQException mqex)
{
System.out.println(mqex);
}
catch (java.io.IOException ioex)
{
System.out.println("An MQ IO error occurred : " + ioex);
}
}
// ===============================================================
//
// Method Name
// ReadFile
//
// Description
// This method will
//
// Input parameters
// None.
//
// Output
// None.
//
// Return Value
// None.
//
// Called Methods
// None.
//
// ---------------------------------------------------------------
private String ReadFile(String fileName)
{
String line = "";
String myMessage = "";
try
{
BufferedReader in = new BufferedReader(new FileReader(fileName));
while ((line = in.readLine()) != null)
{
myMessage += line;
}
in.close();
}
catch (Exception E)
{
System.out.println("ReadFile: Exception: " + E.getMessage());
}
return myMessage;
}
} |
_________________ Capitalware: Transforming tomorrow into today.
Connected to MQ!
Twitter |
|
Back to top |
|
 |
joecherian |
Posted: Wed Mar 05, 2003 10:15 pm Post subject: |
|
|
Novice
Joined: 15 Dec 2002 Posts: 23 Location: India
|
hello
thanks a lot for your help
regards
Joe |
|
Back to top |
|
 |
zpat |
Posted: Thu Mar 06, 2003 1:22 am Post subject: |
|
|
 Jedi Council
Joined: 19 May 2001 Posts: 5866 Location: UK
|
There's also a free support pac which does this. |
|
Back to top |
|
 |
|
|
 |
|
Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|