mqsload/MQSLoad.java
giberta1 61209c439c Fix bad file name print-out,
Improve exeption handling,
Add BufferedInputStream support for file reading.
2002-04-15 10:46:14 +00:00

212 lines
7.2 KiB
Java

// $RCSfile: MQSLoad.java,v $
// $Revision: 1.2 $
// $Name: $
// $Date: 2002/04/15 10:46:14 $
// $Author: giberta1 $
/*
* 07/12/2001 - 11:45:14
*
* MQSLoad.java - MQ/Seires data file to queue loader
* Copyright (C) 2001 Arnaud G. Gibert
* arnaud.gibert@midas-kapiti.fr
* www.midas-kapiti.fr
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
import com.ibm.mq.*;
import java.io.*;
public class MQSLoad
{
private MQQueueManager QMng;
private String QMng_Name = "";
private MQQueue MsgQ;
private String MsgQ_Name;
private int MsgQ_Open_Options = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;
private String File_Name;
public static void main( String args[])
{
new MQSLoad( args);
}
private void MQSInit() throws Exception
{
try
{
QMng = new MQQueueManager( QMng_Name);
System.out.println( "QManager Open: (" + QMng + ") !");
}
catch( Exception Expt)
{
throw Expt;
}
try
{
MsgQ = QMng.accessQueue( MsgQ_Name, MsgQ_Open_Options, null, null, null);
System.out.println( "MsgQ Open: (" + MsgQ + ") !");
}
catch( Exception Expt)
{
QMng.disconnect();
System.out.println( "QManager Close: (" + QMng + ") !");
throw Expt;
}
}
private void MQSDeInit() throws Exception
{
try
{
MsgQ.close();
System.out.println( "MsgQ Close: (" + MsgQ + ") !");
QMng.disconnect();
System.out.println( "QManager Close: (" + QMng + ") !");
}
catch( Exception Expt)
{
throw Expt;
}
}
private void MQSPut_Msg( MQMessage Msg) throws Exception
{
try
{
MQPutMessageOptions pmo = new MQPutMessageOptions();
MsgQ.put( Msg, pmo);
}
catch( Exception Expt)
{
throw Expt;
}
}
public MQSLoad( String args[])
{
byte input_char;
int msg_nb = 0;
MQMessage output_msg = new MQMessage();
try
{
if( args.length != 2)
{
System.out.println( "Usage: MQSLoad <Output_MsgQueue_Name> <Input_File_Name>");
}
else
{
System.out.println( "MQS Load Starting...");
MsgQ_Name = args[0];
File_Name = args[1];
System.out.println( "Output MsgQueue Name: (" + MsgQ_Name + ") Input File Name: (" + File_Name + ")");
MQSInit();
try
{
output_msg.format = MQC.MQFMT_STRING;
BufferedInputStream input_file = new BufferedInputStream( new FileInputStream(File_Name));
try
{
System.out.println("Input File Open: (" + input_file + ") !");
while( ( input_char = (byte)input_file.read()) != -1)
{
switch( input_char)
{
case '\r':
case ';':
{
break;
}
case '\n':
{
System.out.println( "Sending Msg Nb (" + ++msg_nb + ") !");
output_msg.messageId = MQC.MQMI_NONE;
MQSPut_Msg(output_msg);
output_msg.clearMessage();
break;
}
default:
{
output_msg.writeByte( input_char);
break;
}
}
}
}
catch( Exception Expt)
{
input_file.close();
throw Expt;
}
input_file.close();
}
catch( Exception Expt)
{
MQSDeInit();
throw Expt;
}
MQSDeInit();
System.out.println( "MQS Load Completed !");
}
}
catch( Exception Expt)
{
System.out.println("Exception: (" + Expt + ") !");
Expt.printStackTrace();
}
}
}