我正在尝试编写一个简单的Java程序来将MQ消息注入队列。我对使用Java编写MQ队列非常缺乏经验,并且有几个问题。
从我在谷歌中找到的声音中我遗漏了某种资源。我想我可能会收到此错误bc我不允许从Windows连接到队列?
如何实现我正在做或正在帮助的任何好例子都将受到赞赏。
public class MQInject {
private MQQueueManager _queueManager = null;
private Hashtable params = null;
public int port = 1414;
public static final String hostname = "UQMYPOSIS1";
public static final String channel = "MQTX1012.MQTX1013";
public static final String qManager = "MQTX1013";
public static final String outputQName = "IIS.TLOG.5";
public MQInject(){
super();
}
public void init(){
//Set MQ connection credentials to MQ Envorinment.
MQEnvironment.hostname = hostname;
MQEnvironment.channel = channel;
MQEnvironment.port = port;
//MQEnvironment.userID = "";
//QEnvironment.password = password;
//set transport properties.
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
try {
//initialize MQ manager.
_queueManager = new MQQueueManager(qManager);
} catch (MQException e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
MQInject write = new MQInject();
try
{
write.selectQMgr();
write.write();
}
catch (IllegalArgumentException e)
{
System.out.println("Usage: java MQWrite <-h host> <-p port> <-c channel> <-m QueueManagerName> <-q QueueName>");
System.exit(1);
}
catch (MQException e)
{
System.out.println(e);
System.exit(1);
}
}
private void selectQMgr() throws MQException
{
_queueManager = new MQQueueManager(qManager);
}
private void write() throws MQException{
String line;
int lineNum=0;
int openOptions = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING;
try {
MQQueue queue = _queueManager.accessQueue( outputQName,
openOptions,
null, // default q manager
null, // no dynamic q name
null ); // no alternate user id
DataInputStream input = new DataInputStream(System.in);
System.out.println("MQWrite v1.0 connected");
System.out.println("and ready for input, terminate with ^Z\n\n");
// Define a simple MQ message, and write some text in UTF format..
MQMessage sendmsg = new MQMessage();
sendmsg.format = MQC.MQFMT_STRING;
sendmsg.feedback = MQC.MQFB_NONE;
sendmsg.messageType = MQC.MQMT_DATAGRAM;
sendmsg.replyToQueueName = "ROGER.QUEUE";
sendmsg.replyToQueueManagerName = qManager;
MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults, same
// as MQPMO_DEFAULT constant
while ((line = input.readLine()) != null){
sendmsg.clearMessage();
sendmsg.messageId = MQC.MQMI_NONE;
sendmsg.correlationId = MQC.MQCI_NONE;
sendmsg.writeString(line);
// put the message on the queue
queue.put(sendmsg, pmo);
System.out.println(++lineNum + ": " + line);
}
queue.close();
_queueManager.disconnect();
}catch (com.ibm.mq.MQException mqex){
System.out.println(mqex);
}
catch (java.io.IOException ioex){
System.out.println("An MQ IO error occurred : " + ioex);
}
}
}
答案 0 :(得分:1)
对于您的第一个问题,是的,您可以在UNIX主机上运行队列管理器,该主机由运行在Windows主机上的客户端访问。
对于第二个问题,mqjbnd05库仅用于以绑定模式连接到队列管理器(即,当队列管理器和访问队列的程序位于同一主机上时)并且不是MQ客户端安装的一部分。有关详细信息,请参阅http://www-01.ibm.com/support/docview.wss?uid=swg21158430。查看代码,我可以看到init()
函数正在指定MQC.TRANSPORT_MQSERIES_CLIENT
,尽管我看不到正在调用init()
函数。此外,可能值得检查是否在库路径中指定了mqjbnd05,如果是,则将其删除。
虽然可能与您得到的错误无关,但另一件可能值得检查的事情是,频道MQTX1012.MQTX1013
是服务器连接频道,而不是发送方或接收方频道。