Developing
an Applet based Sender/Receiver using MQSeries
Gopalan Suresh Raj
Note |
To
work with any of these samples, you will need the
following: .........................................A Java 1.1 Compatible Compiler .........................................IBM's MQSeries (ver 5.1 or higher) .........................................IBM's MA88 Product Extension for MQSeries |
The Example below, demonstrates developing an Applet based Server and Receiver for sending and receiving messages using IBM's MQSeries product.
The Steps involved in developing the MQSeries Sender/Receiver Applet component are
1. Create a new Server Connection Channel called 'JAVA.CHANNEL' on the Default Queue Manager
2. Develop the Sender/Receiver Applet
3. Build the project
4. Open up the HTML page and view the Applet
1. Create a new
Server Connection Channel called 'JAVA.CHANNEL' on the Default Queue Manager:
2. Develop the
Sender/Receiver Applet
The final code looks like this:
MQApplet.java |
//****************************************************************************** // MQApplet.java: Applet // //****************************************************************************** import java.awt.*; import java.applet.*; import java.awt.event.*; // Include the MQ package import com.ibm.mq.*; public class MQApplet extends Applet implements Runnable, ActionListener { private Button btnCreateQ, btnOpenQ, btnSendMsg, btnRecvMsg; private Label labHName, labChannel, labQMgr; private TextField editHName, editChannel, editQMgr; private TextField editSendMsgText, editRecvMsgText; // define the name of your host to connect to private String m_hostname = "localhost"; // define name of channel for client to use private String m_channel = "JAVA.CHANNEL"; // Note. assumes MQ Server is listening on // the default TCP/IP port of 1414 // define name of queue manager object to connect to. private String m_qManager = "QM_home_pc.home_pc"; MQQueue m_system_default_local_queue; // define a queue manager object private MQQueueManager m_qMgr; private Thread m_mqApplet = null; private boolean m_fStandAlone = false; public MQApplet() { // UI for the applet btnCreateQ=new Button ( "Create Q" ); btnCreateQ.addActionListener(this); btnOpenQ=new Button ( "Open Q" ); btnOpenQ.addActionListener(this); btnSendMsg=new Button ( "Send Msg" ); btnSendMsg.addActionListener(this); btnRecvMsg=new Button ( "Recv Msg" ); btnRecvMsg.addActionListener(this); labHName=new Label ( "Host Name:" ); labChannel=new Label ( "Channel:" ); labQMgr=new Label ( "Queue Manager:" ); editHName=new TextField ( "localhost", 30 ); editChannel=new TextField ( "JAVA.CHANNEL", 30 ); editQMgr=new TextField ( "QM_home_pc.home_pc", 30 ); editSendMsgText=new TextField ( "Test Message", 30 ); editRecvMsgText=new TextField ( "", 30 ); } public String getAppletInfo() { return "Name: MQApplet\r\n" + "Author: Gopalan Suresh Raj" + "Created with Microsoft Visual J++ Version 6.0"; } public static void main (String []args) { MQApplet obj = new MQApplet(); obj.init(); obj.start(); } public void init() { setLayout ( new GridLayout ( 3, 1 )); Panel p=new Panel(); p.setLayout ( new GridLayout ( 3,2 )); p.add ( labHName ); p.add ( editHName); p.add ( labChannel ); p.add ( editChannel ); p.add ( labQMgr ); p.add ( editQMgr ); add ( p ); p=new Panel(); p.setLayout ( new GridLayout ( 3, 2 )); p.add ( btnCreateQ ); p.add ( btnOpenQ ); p.add ( btnSendMsg ); p.add ( editSendMsgText ); p.add ( btnRecvMsg ); p.add ( editRecvMsgText ); add ( p ); resize(600, 400); } public void destroy() { // TODO: Place applet cleanup code here } public void paint(Graphics g) { } public void start() { if (m_mqApplet == null) { m_mqApplet = new Thread(this); m_mqApplet.start(); } // TODO: Place additional applet start code here } public void stop() { if (m_mqApplet != null) { try { // Close the queue m_system_default_local_queue.close(); // Disconnect from the queue manager m_qMgr.disconnect(); } // If an error has occured in the above, try to identify what went wrong. // Was it an MQ error? catch (MQException ex) { System.out.println("An MQ error occurred : Completion code " + ex.completionCode + " Reason code " + ex.reasonCode); } m_mqApplet.stop(); m_mqApplet = null; } // TODO: Place additional applet stop code here } public void run() { while (true) { try { // TODO: Add additional thread-specific code here Thread.sleep(50); } catch (InterruptedException e) { // TODO: Place exception-handling code here in case an // InterruptedException is thrown by Thread.sleep(), // meaning that another thread has interrupted this one e.printStackTrace(); stop(); } } } public void actionPerformed(ActionEvent evt) { if ( evt.getSource() == btnCreateQ ) CreateQueue(); else if ( evt.getSource() == btnOpenQ ) OpenQueue(); else if ( evt.getSource() == btnSendMsg ) SendMsg(); else if ( evt.getSource() == btnRecvMsg ) RecvMsg(); } private void CreateQueue() { try { // Set up MQ environment // Could have put the hostname & channel string directly here! MQEnvironment.hostname = editHName.getText(); MQEnvironment.channel = editChannel.getText(); // Create a connection to the queue manager m_qMgr = new MQQueueManager(editQMgr.getText()); } // If an error has occured in the above, try to identify what went wrong. // Was it an MQ error? catch (MQException ex) { System.out.println("An MQ error occurred : Completion code " + ex.completionCode + " Reason code " + ex.reasonCode); } } private void OpenQueue() { try { // Set up the options on the queue we wish to open... // Note. All 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... m_system_default_local_queue = m_qMgr.accessQueue("SYSTEM.DEFAULT.LOCAL.QUEUE", openOptions, null, // default q manager null, // no dynamic q name null); // no alternate user id } // If an error has occured in the above, try to identify what went wrong. // Was it an MQ error? catch (MQException ex) { System.out.println("An MQ error occurred : Completion code " + ex.completionCode + " Reason code " + ex.reasonCode); } } private void SendMsg() { try { // Define a simple MQ message, and initialise it in UTF format.. MQMessage sendMsg = new MQMessage(); sendMsg.writeUTF(editSendMsgText.getText()); // specify the message options... MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults, same // as MQPMO_DEFAULT constant // put the message on the queue m_system_default_local_queue.put(sendMsg,pmo); } // If an error has occured in the above, try to identify what went wrong. // Was it an MQ error? catch (MQException ex) { System.out.println("An 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); } } private void RecvMsg() { try { // get the message back again... // First define a MQ message buffer to receive the message into.. MQMessage retrievedMessage = new MQMessage(); // retrievedMessage.messageId = hello_world.messageId; editRecvMsgText.setText (""); // clear old text // Set the get message options.. MQGetMessageOptions gmo = new MQGetMessageOptions(); // accept the defaults // same as MQGMO_DEFAULT // get the message off the queue.. m_system_default_local_queue.get(retrievedMessage, gmo, 100); // max message size // And prove we have the message by displaying the UTF message text String msgText = retrievedMessage.readUTF(); editRecvMsgText.setText (msgText); System.out.println("The message is: " + msgText); } // If an error has occured in the above, try to identify what went wrong. // Was it an MQ error? catch (MQException ex) { System.out.println("An 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); } } } |
4. Create the
HTML Code to View the Applet
MQApplet.html |
<HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> </HEAD> <BODY> <P> </P><!-- Insert HTML here --><APPLET code=MQApplet.class height=200 name=MQApplet width=320> <PARAM NAME="foreground" VALUE="FFFFFF"> <PARAM NAME="background" VALUE="008080"> <PARAM NAME="label" VALUE="This string was passed from the HTML host."> </APPLET> </BODY> </HTML> |
5. Open up
AppletViewer and run the applet
This site was developed and is maintained by Gopalan Suresh Raj This page has been visited times since February 24,1999. |
Last Updated : Feb 24, '99 |
Copyright (c) 1997-2000, Gopalan Suresh Raj - All rights reserved. Terms of use. |
All products and companies mentioned at this site are trademarks of their respective owners. |