mq java client 方式和mq java binding方式的区别 Java和MQ问题:JMS中的进入了OnMessage方法,...

MQ Java Client \u65b9\u5f0f\u548cMQ Java Binding\u65b9\u5f0f\u7684\u533a\u522b

\u3000MQ Java Binding\u65b9\u5f0f\u4f7f\u7528JNI(Java Native Interface)\u7c7b\u4f3c\u4e8eMQ \u670d\u52a1\u5668\u5e94\u7528\u7a0b\u5e8f\u3002

\u3000\u3000MQSeries Java\u5ba2\u6237\u673a\u670d\u52a1\u5668\u8fde\u63a5\u6700\u5feb\u7684\u65b9\u5f0f\u662fMQ Java Binding\u65b9\u5f0f\uff0c\u8fd9\u79cd\u65b9\u5f0f\u8981\u6c42MQ Java\u5e94\u7528\u548cMQ Server\u5728\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u3002\u4f7f\u7528MQ Java Binding\u65b9\u5f0f\u907f\u514d\u4e86\u5efa\u7acb\u7f51\u7edc\u8fde\u63a5\u7684\u5f00\u9500\uff0c\u56e0\u6b64\uff0c\u5f53\u8fde\u63a5\u5bf9\u6027\u80fd\u5f71\u54cd\u5f88\u5927\u65f6\uff0c\u5e94\u5f53\u9009\u7528MQ Java Binding\u65b9\u5f0f\u3002

\u3000\u3000MQ Java Client\u65b9\u5f0f\u901a\u8fc7Server\u7aef\u5b9a\u4e49\u7684\u670d\u52a1\u5668\u8fde\u63a5\u901a\u9053\u8fde\u63a5\uff0c\u670d\u52a1\u5668\u65b9\u9700\u8981\u542f\u52a8\u4fa6\u542c\u7a0b\u5e8f\u3002MQ Java Client\u65b9\u5f0f\u7528\u4e8eJava\u5ba2\u6237\u7a0b\u5e8f\u548c\u670d\u52a1\u5668\u4e0d\u5728\u540c\u4e00\u53f0\u673a\u5668\u65f6\u8fdb\u884c\u8fde\u63a5\u3002

\u3000\u3000\u5ba2\u6237\u7aef\u8fde\u63a5\uff0c\u5efa\u7acbMQEnvironment\u7c7b

\u3000\u3000MQEnvironment.hostname

\u3000\u3000\u4ee5\u4e0b\u662f\uff0c\u5ba2\u6237\u7aef\u8fde\u63a5\u4f8b\u5b50

\u3000\u3000// ===========================================================================
\u3000\u3000//
\u3000\u3000// Licensed Materials - Property of IBM
\u3000\u3000//
\u3000\u3000// 5639-C34
\u3000\u3000//
\u3000\u3000// (c) Copyright IBM Corp. 1995,1999
\u3000\u3000//
\u3000\u3000// ===========================================================================
\u3000\u3000// WebSphere MQ M'z Java f sample applet
\u3000\u3000//
\u3000\u3000// This sample runs as an applet using the appletviewer and HTML file,
\u3000\u3000// using the command :-
\u3000\u3000// appletviewer MQSample.html
\u3000\u3000// Output is to the command line, NOT the applet viewer window.
\u3000\u3000//
\u3000\u3000// Note. If you receive WebSphere MQ error 2 reason 2059 and you are sure your
\u3000\u3000// WebSphere MQ and TCP/IPsetup is correct,
\u3000\u3000// you should click on the "Applet" selection in the Applet viewer window
\u3000\u3000// select properties, and change "Network access" to unrestricted.
\u3000\u3000import com.ibm.mq.*; // Include the WebSphere MQ classes for Java package
\u3000\u3000public class MQSample extends java.applet.Applet
\u3000\u3000{
\u3000\u3000private String hostname = "your_hostname"; // define the name of your
\u3000\u3000// host to connect to
\u3000\u3000private String channel = "server_channel"; // define name of channel
\u3000\u3000// for client to use
\u3000\u3000// Note. assumes WebSphere MQ Server
\u3000\u3000// is listening on the default
\u3000\u3000// TCP/IPport of 1414
\u3000\u3000private String qManager = "your_Q_manager"; // define name of queue
\u3000\u3000// manager object to
\u3000\u3000// connect to.
\u3000\u3000private MQQueueManager qMgr; // define a queue manager object
\u3000\u3000// When the class is called, this initialization is done first.
\u3000\u3000public void init()
\u3000\u3000{
\u3000\u3000// Set up WebSphere MQ environment
\u3000\u3000MQEnvironment.hostname = hostname; // Could have put the
\u3000\u3000// hostname & channel
\u3000\u3000MQEnvironment.channel = channel; // string directly here!
\u3000\u3000MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,//Set TCP/IPor server
\u3000\u3000MQC.TRANSPORT_MQSERIES);//Connection
\u3000\u3000} // end of init

\u3000\u3000public void start()
\u3000\u3000{
\u3000\u3000try {
\u3000\u3000// Create a connection to the queue manager
\u3000\u3000qMgr = new MQQueueManager(qManager);
\u3000\u3000// Set up the options on the queue we wish to open...
\u3000\u3000// Note. All WebSphere MQ Options are prefixed with MQC in Java.
\u3000\u3000int openOptions = MQC.MQOO_INPUT_AS_Q_DEF |
\u3000\u3000MQC.MQOO_OUTPUT ;
\u3000\u3000// Now specify the queue that we wish to open, and the open options...
\u3000\u3000MQQueue system_default_local_queue =
\u3000\u3000qMgr.accessQueue("SYSTEM.DEFAULT.LOCAL.QUEUE",
\u3000\u3000openOptions);
\u3000\u3000// Define a simple WebSphere MQ message, and write some text in UTF format..
\u3000\u3000MQMessage hello_world = new MQMessage();
\u3000\u3000hello_world.writeUTF("Hello World!");
\u3000\u3000// specify the message options...
\u3000\u3000MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults,
\u3000\u3000// same as
\u3000\u3000// MQPMO_DEFAULT
\u3000\u3000// constant
\u3000\u3000// put the message on the queue
\u3000\u3000system_default_local_queue.put(hello_world,pmo);
\u3000\u3000// get the message back again...
\u3000\u3000// First define WebSphere MQ message buffer to receive the message into..
\u3000\u3000MQMessage retrievedMessage = new MQMessage();
\u3000\u3000retrievedMessage.messageId = hello_world.messageId;
\u3000\u3000// Set the get message options..
\u3000\u3000MQGetMessageOptions gmo = new MQGetMessageOptions(); // accept the defaults
\u3000\u3000// same as
\u3000\u3000// MQGMO_DEFAULT
\u3000\u3000// get the message off the queue..
\u3000\u3000system_default_local_queue.get(retrievedMessage, gmo);
\u3000\u3000// And prove we have the message by displaying the UTF message text
\u3000\u3000String msgText = retrievedMessage.readUTF();
\u3000\u3000System.out.println("The message is: " + msgText);
\u3000\u3000// Close the queue
\u3000\u3000system_default_local_queue.close();
\u3000\u3000// Disconnect from the queue manager
\u3000\u3000qMgr.disconnect();
\u3000\u3000}
\u3000\u3000// If an error has occurred in the above, try to identify what went wrong.
\u3000\u3000// Was it WebSphere MQ error?
\u3000\u3000} applet (2/3)
\u3000\u3000>}zk
\u3000\u300062 WebSphere MQ 9C Java
\u3000\u3000>}
\u3000\u3000TBzkN]>;vr%D&CLr,|9Cs(==:
\u3000\u30001. ,S=SP\mw
\u3000\u30002. +{"Ek SYSTEM.DEFAULT.LOCAL.QUEUE
\u3000\u30003. YN!5XD{"
\u3000\u3000catch (MQException ex)
\u3000\u3000{
\u3000\u3000System.out.println("WebSphere MQ error occurred : Completion code " +
\u3000\u3000ex.completionCode +
\u3000\u3000" Reason code " + ex.reasonCode);
\u3000\u3000}
\u3000\u3000// Was it a Java buffer space error?
\u3000\u3000catch (java.io.IOException ex)
\u3000\u3000{
\u3000\u3000System.out.println("An error occurred whilst writing to the
\u3000\u3000message buffer: " + ex);
\u3000\u3000}
\u3000\u3000} // end of start
\u3000\u3000} // end of sample

MQ\u6709\u4e24\u79cd\u6d88\u606f\uff1aqueue\u548ctopic

\u5982\u679c\u7528queue\u7684\u8bdd\u53ea\u4f1a\u6709\u4e00\u4e2a\u88ab\u6d88\u606f\uff1btopic\u90fd\u6709\u6d88\u8d39

  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

扩展阅读:java windowbuilder ... liquipedia dota2 ... macbook pro 14 ... macbook air pro ... minecraft ... a deaf disc jockey ... java入门网站 ... gac filipaj ... clone app pro ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网