/[projects]/dao/file2mq/file2mq/Program.cs
ViewVC logotype

Contents of /dao/file2mq/file2mq/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1984 - (show annotations) (download)
Wed Jul 3 07:51:12 2013 UTC (10 years, 10 months ago) by torben
File size: 5727 byte(s)
Add file2mq project files
1 // ===========================================================================
2 using System;
3 using System.IO;
4 using System.Collections;
5 using System.Text;
6
7 using IBM.WMQ;
8
9 class File2Mq
10 {
11 // The type of connection to use, this can be:-
12 // MQC.TRANSPORT_MQSERIES_BINDINGS for a server connection.
13 // MQC.TRANSPORT_MQSERIES_CLIENT for a non-XA client connection
14 // MQC.TRANSPORT_MQSERIES_XACLIENT for an XA client connection
15 // MQC.TRANSPORT_MQSERIES_MANAGED for a managed client connection
16 const String connectionType = MQC.TRANSPORT_MQSERIES_CLIENT;
17
18 // Define the name of the channel to use (applies to client connections only)
19 const String channel = "CLIENT";
20
21
22
23 static Hashtable init(String connectionType, string hostName)
24 {
25 Hashtable connectionProperties = new Hashtable();
26
27 // Add the connection type
28 connectionProperties.Add(MQC.TRANSPORT_PROPERTY, connectionType);
29
30 // Set up the rest of the connection properties, based on the
31 // connection type requested
32 switch (connectionType)
33 {
34 case MQC.TRANSPORT_MQSERIES_BINDINGS:
35 break;
36 case MQC.TRANSPORT_MQSERIES_CLIENT:
37 case MQC.TRANSPORT_MQSERIES_XACLIENT:
38 case MQC.TRANSPORT_MQSERIES_MANAGED:
39 connectionProperties.Add(MQC.HOST_NAME_PROPERTY, hostName);
40 connectionProperties.Add(MQC.CHANNEL_PROPERTY, channel);
41 break;
42 }
43
44 return connectionProperties;
45 }
46
47 [STAThread]
48 static int Main(string[] args)
49 {
50
51 if (args.Length != 5)
52 {
53 Console.WriteLine("Usage: file2mq.exe <server> <queuemanager> <queuename> <file> <archivedir>");
54 Console.WriteLine("Ex: file2mq.exe 10.30.1.26 DAOP KISS.IND d:\\data\\test.file d:\\data\\old");
55 Environment.Exit(0);
56 }
57
58 String hostName = args[0];
59 String queueManager = args[1];
60 String queueName = args[2];
61 String fileName = args[3];
62 String archiveDir = args[4];
63
64 if (File.Exists(fileName) == false)
65 {
66 Console.WriteLine("File does not exists {0}", fileName);
67 Environment.Exit(1);
68 }
69
70 if (Directory.Exists(archiveDir) == false)
71 {
72 Console.WriteLine("Archive Directory does not exists {0}", archiveDir);
73 Environment.Exit(1);
74 }
75
76
77 StreamReader input = null;
78 try
79 {
80 Encoding iso = Encoding.GetEncoding("iso8859-1");
81 input = new StreamReader (fileName, iso);
82 }
83 catch (Exception ex)
84 {
85 Console.WriteLine("Error opening file: {0}", ex.ToString());
86 Environment.Exit(1);
87 }
88
89
90 try
91 {
92 Hashtable connectionProperties = init(connectionType, hostName);
93
94 // Create a connection to the queue manager using the connection
95 // properties just defined
96 MQQueueManager qMgr = new MQQueueManager(queueManager, connectionProperties);
97
98 // Set up the options on the queue we want to open
99 //int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
100 int openOptions = MQC.MQOO_OUTPUT;
101
102 // Now specify the queue that we want to open,and the open options
103 MQQueue out_queue = qMgr.AccessQueue( queueName, openOptions);
104
105
106 // Specify the message options
107
108 while (input.EndOfStream == false)
109 {
110 string line = input.ReadLine();
111 line = line.Trim();
112
113 if (line.Length == 0) {//put ikke tomme linier i mq
114 continue;
115 }
116
117 MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults,
118 // same as MQPMO_DEFAULT
119
120 MQMessage msg = new MQMessage();
121 msg.Format = MQC.MQFMT_STRING;
122 //msg.CharacterSet = 819; //iso 8859-1
123 msg.CharacterSet = 1252; //iso 8859-1
124 msg.WriteString(line);
125
126 out_queue.Put(msg, pmo);
127
128 }
129
130 // Close the queue
131 out_queue.Close();
132
133 // Disconnect from the queue manager
134 qMgr.Disconnect();
135
136 input.Close();
137
138 String baseName = Path.GetFileName(fileName);
139 Directory.Move(fileName, archiveDir + "\\" + baseName);
140 }
141
142 //If an error has occurred in the above,try to identify what went wrong.
143 //Was it a WebSphere MQ error?
144 catch (MQException ex)
145 {
146 Console.WriteLine("A WebSphere MQ error occurred: {0}", ex.ToString());
147 Environment.Exit(1);
148 }
149
150 catch (System.Exception ex)
151 {
152 Console.WriteLine("A System error occurred: {0}", ex.ToString());
153 Environment.Exit(1);
154 }
155
156
157 return 0;
158 }
159 }
160
161 /*// Get the message back again
162
163 // First define a WebSphere MQ message buffer to receive the message
164 MQMessage retrievedMessage = new MQMessage();
165 retrievedMessage.MessageId = hello_world.MessageId;
166
167 // Set the get message options
168 MQGetMessageOptions gmo = new MQGetMessageOptions(); //accept the defaults
169 //same as MQGMO_DEFAULT
170
171 // Get the message off the queue
172 kiss_ind_queue.Get(retrievedMessage, gmo);
173
174 // Prove we have the message by displaying the UTF message text
175 String msgText = retrievedMessage.ReadUTF();
176 Console.WriteLine("The message is: {0}", msgText);*/

  ViewVC Help
Powered by ViewVC 1.1.20