java 编写一个简单的应用程序将消息注入队列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16031243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing a simple application to inject messages onto a queue
提问by Richie
I'm trying to write a simple Java program to inject an MQ message onto a queue. I'm very inexperienced with writing to MQ queues using Java and have a couple of questions.
我正在尝试编写一个简单的 Java 程序来将 MQ 消息注入队列。我对使用 Java 写入 MQ 队列非常缺乏经验,并且有几个问题。
- Can I connect to the unix queue on the unix box from my windows machine?
- When I try to run the application I get a .... java.lang.UnsatisfiedLinkError: no mqjbnd05 in java.library.path
- 我可以从我的 Windows 机器连接到 unix box 上的 unix 队列吗?
- 当我尝试运行应用程序时,我得到一个 .... java.lang.UnsatisfiedLinkError: no mqjbnd05 in java.library.path
From the sounds of what I could find in google I am missing some sort of resource. I'm thinking I'm getting this error possibly bc I'm not allowed to connect to the queue from windows?
从我在谷歌中可以找到的声音来看,我缺少某种资源。我想我可能会收到这个错误,因为我不允许从 Windows 连接到队列?
Any good examples of how to achieve what I'm doing or help would be appreciated.
任何关于如何实现我正在做的事情或帮助的好例子都将不胜感激。
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);
}
}
}
回答by Paul H
For your first question, yes you can have a queue manager running on a UNIX host which is accessed by a client running on a Windows host.
对于您的第一个问题,是的,您可以在 UNIX 主机上运行队列管理器,在 Windows 主机上运行的客户端可以访问该队列管理器。
For your second question, the mqjbnd05 library is only used to connect to the queue manager in binding mode (i.e. when the queue manager and the program accessing the queues are on the same host) and is not part of the MQ client installation. See http://www-01.ibm.com/support/docview.wss?uid=swg21158430for more details. Looking through your code I can see that the init()
function is specifying MQC.TRANSPORT_MQSERIES_CLIENT
although I cannot see that the init()
function is being called. Also it might be worth checking whether mqjbnd05 is specified in the library path and if so removing it.
对于您的第二个问题,mqjbnd05 库仅用于以绑定模式连接到队列管理器(即当队列管理器和访问队列的程序在同一主机上时),而不是 MQ 客户端安装的一部分。有关更多详细信息,请参阅http://www-01.ibm.com/support/docview.wss?uid=swg21158430。查看您的代码,我可以看到该init()
函数正在指定,MQC.TRANSPORT_MQSERIES_CLIENT
尽管我看不到init()
正在调用该函数。此外,可能值得检查是否在库路径中指定了 mqjbnd05,如果是,则将其删除。
Whilst probably not related to the error your getting, one other thing that might be worth checking is that the channel MQTX1012.MQTX1013
is a server connection channel as opposed to a sender or receiver channel.
虽然可能与您得到的错误无关,但值得检查的另一件事是该通道MQTX1012.MQTX1013
是服务器连接通道,而不是发送方或接收方通道。