ããMQ Java Bindingæ¹å¼ä½¿ç¨JNI(Java Native Interface)类似äºMQ æå¡å¨åºç¨ç¨åºã
ããMQSeries Java客æ·æºæå¡å¨è¿æ¥æå¿«çæ¹å¼æ¯MQ Java Bindingæ¹å¼ï¼è¿ç§æ¹å¼è¦æ±MQ Javaåºç¨åMQ Serverå¨åä¸å°æºå¨ä¸ã使ç¨MQ Java Bindingæ¹å¼é¿å
äºå»ºç«ç½ç»è¿æ¥çå¼éï¼å æ¤ï¼å½è¿æ¥å¯¹æ§è½å½±åå¾å¤§æ¶ï¼åºå½éç¨MQ Java Bindingæ¹å¼ã
ããMQ Java Clientæ¹å¼éè¿Server端å®ä¹çæå¡å¨è¿æ¥ééè¿æ¥ï¼æå¡å¨æ¹éè¦å¯å¨ä¾¦å¬ç¨åºãMQ Java Clientæ¹å¼ç¨äºJava客æ·ç¨åºåæå¡å¨ä¸å¨åä¸å°æºå¨æ¶è¿è¡è¿æ¥ã
ãã客æ·ç«¯è¿æ¥ï¼å»ºç«MQEnvironmentç±»
ããMQEnvironment.hostname
ãã以ä¸æ¯ï¼å®¢æ·ç«¯è¿æ¥ä¾å
ãã// ===========================================================================
ãã//
ãã// Licensed Materials - Property of IBM
ãã//
ãã// 5639-C34
ãã//
ãã// (c) Copyright IBM Corp. 1995,1999
ãã//
ãã// ===========================================================================
ãã// WebSphere MQ M'z Java f sample applet
ãã//
ãã// This sample runs as an applet using the appletviewer and HTML file,
ãã// using the command :-
ãã// appletviewer MQSample.html
ãã// Output is to the command line, NOT the applet viewer window.
ãã//
ãã// Note. If you receive WebSphere MQ error 2 reason 2059 and you are sure your
ãã// WebSphere MQ and TCP/IP setup is correct,
ãã// you should click on the "Applet" selection in the Applet viewer window
ãã// select properties, and change "Network access" to unrestricted.
ããimport com.ibm.mq.*; // Include the WebSphere MQ classes for Java package
ããpublic class MQSample extends java.applet.Applet
ãã{
ããprivate String hostname = "your_hostname"; // define the name of your
ãã// host to connect to
ããprivate String channel = "server_channel"; // define name of channel
ãã// for client to use
ãã// Note. assumes WebSphere MQ Server
ãã// is listening on the default
ãã// TCP/IP port of 1414
ããprivate String qManager = "your_Q_manager"; // define name of queue
ãã// manager object to
ãã// connect to.
ããprivate MQQueueManager qMgr; // define a queue manager object
ãã// When the class is called, this initialization is done first.
ããpublic void init()
ãã{
ãã// Set up WebSphere MQ environment
ããMQEnvironment.hostname = hostname; // Could have put the
ãã// hostname & channel
ããMQEnvironment.channel = channel; // string directly here!
ããMQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,//Set TCP/IP or server
ããMQC.TRANSPORT_MQSERIES);//Connection
ãã} // end of init
ããpublic void start()
ãã{
ããtry {
ãã// Create a connection to the queue manager
ããqMgr = new MQQueueManager(qManager);
ãã// Set up the options on the queue we wish to open...
ãã// Note. All WebSphere MQ Options are prefixed with MQC in Java.
ããint openOptions = MQC.MQOO_INPUT_AS_Q_DEF |
ããMQC.MQOO_OUTPUT ;
ãã// Now specify the queue that we wish to open, and the open options...
ããMQQueue system_default_local_queue =
ããqMgr.accessQueue("SYSTEM.DEFAULT.LOCAL.QUEUE",
ããopenOptions);
ãã// Define a simple WebSphere MQ message, and write some text in UTF format..
ããMQMessage hello_world = new MQMessage();
ããhello_world.writeUTF("Hello World!");
ãã// specify the message options...
ããMQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults,
ãã// same as
ãã// MQPMO_DEFAULT
ãã// constant
ãã// put the message on the queue
ããsystem_default_local_queue.put(hello_world,pmo);
ãã// get the message back again...
ãã// First define WebSphere MQ message buffer to receive the message into..
ãã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..
ããsystem_default_local_queue.get(retrievedMessage, gmo);
ãã// And prove we have the message by displaying the UTF message text
ããString msgText = retrievedMessage.readUTF();
ããSystem.out.println("The message is: " + msgText);
ãã// Close the queue
ããsystem_default_local_queue.close();
ãã// Disconnect from the queue manager
ããqMgr.disconnect();
ãã}
ãã// If an error has occurred in the above, try to identify what went wrong.
ãã// Was it WebSphere MQ error?
ãã< 1. WebSphere MQ classes for Java >} applet (2/3)
ãã>}zk
ãã62 WebSphere MQ 9C Java
ãã>}&CLrzk
ããTBzkN]>;vr%D&CLr,|9Cs(==:
ãã1. ,S=SP\mw
ãã2. +{"Ek SYSTEM.DEFAULT.LOCAL.QUEUE
ãã3. YN!5XD{"
ããcatch (MQException ex)
ãã{
ããSystem.out.println("WebSphere MQ error occurred : Completion code " +
ããex.completionCode +
ãã" Reason code " + ex.reasonCode);
ãã}
ãã// Was it a Java buffer space error?
ããcatch (java.io.IOException ex)
ãã{
ããSystem.out.println("An error occurred whilst writing to the
ããmessage buffer: " + ex);
ãã}
ãã} // end of start
ãã} // end of sample
温馨提示:答案为网友推荐,仅供参考