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 |
String baseName = Path.GetFileName(fileName);
|
77 |
String archiveFile = archiveDir + "\\" + baseName;
|
78 |
|
79 |
if (File.Exists(archiveFile))
|
80 |
{
|
81 |
Console.WriteLine("Archive file already exists {0}", archiveFile);
|
82 |
Environment.Exit(1);
|
83 |
}
|
84 |
|
85 |
|
86 |
StreamReader input = null;
|
87 |
try
|
88 |
{
|
89 |
Encoding iso = Encoding.GetEncoding("iso8859-1");
|
90 |
input = new StreamReader (fileName, iso);
|
91 |
}
|
92 |
catch (Exception ex)
|
93 |
{
|
94 |
Console.WriteLine("Error opening file: {0}", ex.ToString());
|
95 |
Environment.Exit(1);
|
96 |
}
|
97 |
|
98 |
|
99 |
try
|
100 |
{
|
101 |
Hashtable connectionProperties = init(connectionType, hostName);
|
102 |
|
103 |
// Create a connection to the queue manager using the connection
|
104 |
// properties just defined
|
105 |
MQQueueManager qMgr = new MQQueueManager(queueManager, connectionProperties);
|
106 |
|
107 |
// Set up the options on the queue we want to open
|
108 |
//int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
|
109 |
int openOptions = MQC.MQOO_OUTPUT;
|
110 |
|
111 |
// Now specify the queue that we want to open,and the open options
|
112 |
MQQueue out_queue = qMgr.AccessQueue( queueName, openOptions);
|
113 |
|
114 |
|
115 |
// Specify the message options
|
116 |
|
117 |
while (input.EndOfStream == false)
|
118 |
{
|
119 |
string line = input.ReadLine();
|
120 |
line = line.Trim();
|
121 |
|
122 |
if (line.Length == 0) {//put ikke tomme linier i mq
|
123 |
continue;
|
124 |
}
|
125 |
|
126 |
MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults,
|
127 |
// same as MQPMO_DEFAULT
|
128 |
|
129 |
MQMessage msg = new MQMessage();
|
130 |
msg.Format = MQC.MQFMT_STRING;
|
131 |
//msg.CharacterSet = 819; //iso 8859-1
|
132 |
msg.CharacterSet = 1252; //iso 8859-1
|
133 |
msg.WriteString(line);
|
134 |
|
135 |
out_queue.Put(msg, pmo);
|
136 |
|
137 |
}
|
138 |
|
139 |
// Close the queue
|
140 |
out_queue.Close();
|
141 |
|
142 |
// Disconnect from the queue manager
|
143 |
qMgr.Disconnect();
|
144 |
|
145 |
input.Close();
|
146 |
|
147 |
|
148 |
Directory.Move(fileName, archiveFile);
|
149 |
}
|
150 |
|
151 |
//If an error has occurred in the above,try to identify what went wrong.
|
152 |
//Was it a WebSphere MQ error?
|
153 |
catch (MQException ex)
|
154 |
{
|
155 |
Console.WriteLine("A WebSphere MQ error occurred: {0}", ex.ToString());
|
156 |
Environment.Exit(1);
|
157 |
}
|
158 |
|
159 |
catch (System.Exception ex)
|
160 |
{
|
161 |
Console.WriteLine("A System error occurred: {0}", ex.ToString());
|
162 |
Environment.Exit(1);
|
163 |
}
|
164 |
|
165 |
|
166 |
return 0;
|
167 |
}
|
168 |
}
|
169 |
|
170 |
/*// Get the message back again
|
171 |
|
172 |
// First define a WebSphere MQ message buffer to receive the message
|
173 |
MQMessage retrievedMessage = new MQMessage();
|
174 |
retrievedMessage.MessageId = hello_world.MessageId;
|
175 |
|
176 |
// Set the get message options
|
177 |
MQGetMessageOptions gmo = new MQGetMessageOptions(); //accept the defaults
|
178 |
//same as MQGMO_DEFAULT
|
179 |
|
180 |
// Get the message off the queue
|
181 |
kiss_ind_queue.Get(retrievedMessage, gmo);
|
182 |
|
183 |
// Prove we have the message by displaying the UTF message text
|
184 |
String msgText = retrievedMessage.ReadUTF();
|
185 |
Console.WriteLine("The message is: {0}", msgText);*/
|