// =========================================================================== using System; using System.IO; using System.Collections; using System.Text; using IBM.WMQ; class File2Mq { // The type of connection to use, this can be:- // MQC.TRANSPORT_MQSERIES_BINDINGS for a server connection. // MQC.TRANSPORT_MQSERIES_CLIENT for a non-XA client connection // MQC.TRANSPORT_MQSERIES_XACLIENT for an XA client connection // MQC.TRANSPORT_MQSERIES_MANAGED for a managed client connection const String connectionType = MQC.TRANSPORT_MQSERIES_CLIENT; // Define the name of the channel to use (applies to client connections only) const String channel = "CLIENT"; static Hashtable init(String connectionType, string hostName) { Hashtable connectionProperties = new Hashtable(); // Add the connection type connectionProperties.Add(MQC.TRANSPORT_PROPERTY, connectionType); // Set up the rest of the connection properties, based on the // connection type requested switch (connectionType) { case MQC.TRANSPORT_MQSERIES_BINDINGS: break; case MQC.TRANSPORT_MQSERIES_CLIENT: case MQC.TRANSPORT_MQSERIES_XACLIENT: case MQC.TRANSPORT_MQSERIES_MANAGED: connectionProperties.Add(MQC.HOST_NAME_PROPERTY, hostName); connectionProperties.Add(MQC.CHANNEL_PROPERTY, channel); break; } return connectionProperties; } [STAThread] static int Main(string[] args) { if (args.Length != 5) { Console.WriteLine("Usage: file2mq.exe "); Console.WriteLine("Ex: file2mq.exe 10.30.1.26 DAOP KISS.IND d:\\data\\test.file d:\\data\\old"); Environment.Exit(0); } String hostName = args[0]; String queueManager = args[1]; String queueName = args[2]; String fileName = args[3]; String archiveDir = args[4]; if (File.Exists(fileName) == false) { Console.WriteLine("File does not exists {0}", fileName); Environment.Exit(1); } if (Directory.Exists(archiveDir) == false) { Console.WriteLine("Archive Directory does not exists {0}", archiveDir); Environment.Exit(1); } String baseName = Path.GetFileName(fileName); String archiveFile = archiveDir + "\\" + baseName; if (File.Exists(archiveFile)) { Console.WriteLine("Archive file already exists {0}", archiveFile); Environment.Exit(1); } StreamReader input = null; try { Encoding iso = Encoding.GetEncoding("iso8859-1"); input = new StreamReader (fileName, iso); } catch (Exception ex) { Console.WriteLine("Error opening file: {0}", ex.ToString()); Environment.Exit(1); } try { Hashtable connectionProperties = init(connectionType, hostName); // Create a connection to the queue manager using the connection // properties just defined MQQueueManager qMgr = new MQQueueManager(queueManager, connectionProperties); // Set up the options on the queue we want to open //int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT; int openOptions = MQC.MQOO_OUTPUT; // Now specify the queue that we want to open,and the open options MQQueue out_queue = qMgr.AccessQueue( queueName, openOptions); // Specify the message options while (input.EndOfStream == false) { string line = input.ReadLine(); line = line.Trim(); if (line.Length == 0) {//put ikke tomme linier i mq continue; } MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults, // same as MQPMO_DEFAULT MQMessage msg = new MQMessage(); msg.Format = MQC.MQFMT_STRING; //msg.CharacterSet = 819; //iso 8859-1 msg.CharacterSet = 1252; //iso 8859-1 msg.WriteString(line); out_queue.Put(msg, pmo); } // Close the queue out_queue.Close(); // Disconnect from the queue manager qMgr.Disconnect(); input.Close(); Directory.Move(fileName, archiveFile); } //If an error has occurred in the above,try to identify what went wrong. //Was it a WebSphere MQ error? catch (MQException ex) { Console.WriteLine("A WebSphere MQ error occurred: {0}", ex.ToString()); Environment.Exit(1); } catch (System.Exception ex) { Console.WriteLine("A System error occurred: {0}", ex.ToString()); Environment.Exit(1); } return 0; } } /*// Get the message back again // First define a WebSphere MQ message buffer to receive the message MQMessage retrievedMessage = new MQMessage(); retrievedMessage.MessageId = hello_world.MessageId; // Set the get message options MQGetMessageOptions gmo = new MQGetMessageOptions(); //accept the defaults //same as MQGMO_DEFAULT // Get the message off the queue kiss_ind_queue.Get(retrievedMessage, gmo); // Prove we have the message by displaying the UTF message text String msgText = retrievedMessage.ReadUTF(); Console.WriteLine("The message is: {0}", msgText);*/