mqssave/MQSSave.java

525 lines
16 KiB
Java
Raw Normal View History

2002-03-18 10:24:46 +01:00
// $RCSfile: MQSSave.java,v $
// $Revision: 1.2 $
2002-03-18 10:24:46 +01:00
// $Name: $
// $Date: 2002/04/23 14:41:38 $
2002-03-18 10:24:46 +01:00
// $Author: giberta1 $
/*
* 07/12/2001 - 11:45:14
*
* MQSSave.java - MQ/Seires queue to data file saver
* Copyright (C) 2002 Arnaud G. Gibert
* arnaud.gibert@misys.com
* www.misys.com
*
* 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.*;
import java.text.*;
2002-03-18 10:24:46 +01:00
public class MQSSave
{
private MQQueueManager QMng;
private String QMng_Name = "";
private MQQueue MsgQ;
private String MsgQ_Name;
private int MsgQ_Open_Options = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_BROWSE | MQC.MQOO_INQUIRE;
private String File_Name;
private String Message_Break = "\r\n";
//-------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------
public static void main( String args[])
{
new MQSSave( args);
}
//-------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------
public static String Str_Format( String UnFmt)
{
int idx;
boolean esc = false;
String fmt = "";
for( idx = 0; idx < UnFmt.length(); idx++)
{
if( esc)
{
switch( UnFmt.charAt( idx))
{
case '\\':
{
fmt += "\\";
break;
}
case 'n':
{
fmt += "\n";
break;
}
case 'r':
{
fmt += "\r";
break;
}
case 't':
{
fmt += "\t";
break;
}
default:
{
fmt += "?";
break;
}
}
esc = false;
}
else
{
if( UnFmt.charAt( idx) == '\\')
{
esc = true;
}
else
{
fmt += UnFmt.charAt( idx);
}
}
}
return( fmt);
}
//-------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------
public static String Str_UnFormat( String Fmt)
{
int idx;
String unfmt = "";
for( idx = 0; idx < Fmt.length(); idx++)
{
switch( Fmt.charAt( idx))
{
case '\\':
{
unfmt += "\\\\";
break;
}
case '\n':
{
unfmt += "\\n";
break;
}
case '\r':
{
unfmt += "\\r";
break;
}
case '\t':
{
unfmt += "\\t";
break;
}
default:
{
unfmt += Fmt.charAt( idx);
break;
}
}
}
return( unfmt);
}
//-------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------
private void Arg_Parse( String args[]) throws Exception
{
int argc = 0;
try
{
while( argc < args.length)
{
if ( args[argc].equals( "-mb"))
{
if( argc < ( args.length + 1))
{
Message_Break = Str_Format( args[++argc]);
}
else
{
System.out.println( "Invalid number of command line options...");
throw new Exception();
}
}
else
{
File_Name = args[argc++];
MsgQ_Name = args[argc++];
if( argc < args.length)
{
System.out.println( "Invalid number of command line options...");
throw new Exception();
}
}
++argc;
}
}
catch( Exception Expt)
{
throw Expt;
}
}
//-------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------
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 MQMessage MQSGet_Msg( MQMessage Msg) throws Exception
{
MQGetMessageOptions gmo = new MQGetMessageOptions();
try
{
if( MsgQ.getCurrentDepth() > 0)
{
gmo.options = MQC.MQGMO_NO_WAIT | MQC.MQGMO_BROWSE_FIRST | MQC.MQGMO_CONVERT;
gmo.matchOptions = MQC.MQMO_NONE;
MsgQ.get( Msg, gmo);
}
else
{
Msg = null;
}
return( Msg);
}
catch( Exception Expt)
{
//System.out.println("Exception: (" + Expt + ") !!!");
throw Expt;
}
}
//-------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------
private void MQSDelete_Msg( MQMessage Msg) throws Exception
{
MQGetMessageOptions gmo = new MQGetMessageOptions();
try
{
gmo.options = MQC.MQGMO_NO_WAIT | MQC.MQGMO_CONVERT;
gmo.matchOptions = MQC.MQMO_MATCH_MSG_ID;
MsgQ.get( Msg, gmo);
}
catch( Exception Expt)
{
throw Expt;
}
}
//-------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------
private void Counter_Print( int Msg_Nb)
{
if( ( Msg_Nb % 50) == 0)
{
System.out.print( "\n(" + Msg_Nb + ")\t");
}
if( ( Msg_Nb % 10) == 0)
{
System.out.print( " ");
}
System.out.print( ".");
}
//-------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------
private int Save_File( BufferedOutputStream Output_File) throws Exception
{
int msg_nb = 0;
MQMessage input_msg = new MQMessage();
byte[] buffer_byte;
try
{
input_msg.format = MQC.MQFMT_STRING;
do
{
input_msg = MQSGet_Msg( input_msg);
if( input_msg != null)
{
Counter_Print( msg_nb++);
if( msg_nb > 1)
{
Output_File.write( Message_Break.getBytes());
}
buffer_byte = new byte[ input_msg.getMessageLength()];
input_msg.readFully( buffer_byte);
Output_File.write( buffer_byte, 0, buffer_byte.length);
MQSDelete_Msg( input_msg);
}
}
while( input_msg != null);
System.out.println( "\n");
return( msg_nb);
}
catch( Exception Expt)
{
// System.out.println("Exception: (" + Expt + ") !!!");
throw Expt;
}
}
//-------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------
public MQSSave( String args[])
{
int msg_nb;
long time_begin, time_end;
double time_elapsed, speed;
NumberFormat nf = NumberFormat.getInstance();
try
{
if( ( args.length < 2) || ( args.length > 4))
{
System.out.println( "Usage: MQSLoad [-mb \"message_break\"] <Output_File_Name> <Input_MsgQueue_Name>");
System.out.println( " Default: Message Break: (" + Str_UnFormat( Message_Break) + ")");
}
else
{
System.out.println( "MQS Save Starting...");
Arg_Parse( args);
System.out.println( "Output File Name: (" + File_Name + ") Input MsgQue Name: (" + MsgQ_Name + ")");
System.out.println( "Message Break: (" + Str_UnFormat( Message_Break) + ")");
MQSInit();
try
{
BufferedOutputStream Output_file = new BufferedOutputStream( new FileOutputStream( File_Name));
try
{
// System.out.println("Output File Open: (" + Output_file + ") !");
time_begin = System.currentTimeMillis();
msg_nb = Save_File( Output_file);
time_end = System.currentTimeMillis();
time_elapsed = ( time_end - time_begin) / 100.0;
speed = msg_nb / time_elapsed;
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
System.out.println( "Saved Message Nb: (" + msg_nb
+ ") Elapsed Time: (" + nf.format(time_elapsed)
+ ") s Speed: (" + nf.format(speed)
+ ") msg/s");
}
catch( Exception Expt)
{
Output_file.close();
throw Expt;
}
Output_file.close();
}
catch( Exception Expt)
{
MQSDeInit();
throw Expt;
}
MQSDeInit();
System.out.println( "MQS Save Completed !");
}
}
catch( Exception Expt)
{
System.out.println("Exception: (" + Expt + ") !");
Expt.printStackTrace();
}
}
2002-03-18 10:24:46 +01:00
}