Mega Code Archive

 
Categories / Java / J2EE
 

This example illustrates how JMS (Java Message Service) API can be used in a Java applet

/*  * @(#)MQApplet.java  1.1 10/10/03  *  * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.  *  * Sun grants you ("Licensee") a non-exclusive, royalty free, license  * to use, modify and redistribute this software in source and binary  * code form, provided that i) this copyright notice and license  * appear on all copies of the software; and ii) Licensee does not  * utilize the software in a manner which is disparaging to Sun.  *  * This software is provided "AS IS," without a warranty of any kind.  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY  * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE  * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS  * BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,  * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING  * OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.  *  * This software is not designed or intended for use in on-line  * control of aircraft, air traffic, aircraft navigation or aircraft  * communications; or in the design, construction, operation or  * maintenance of any nuclear facility. Licensee represents and  * warrants that it will not use or redistribute the Software for such  * purposes.  */ ================================================================================ @(#)README  1.6 03/22/05 ================================================================================ JMS Applet example Description ----------- This example illustrates how JMS API can be used in a Java applet. MQApplet is a simple chat applet. It can be used with any Java enabled browser. Files ----- MQApplet.java   Source file for this example. mqapplet.html   HTML Source file for the applet. README          This file. build.xml       Build rules (for Jakarta Ant). Configuring the environment --------------------------- To recompile or run this example, you may need to modify the build.xml file. Please change the value of the "libdir" property as appropriate. It should point to the directory that contains the "imq.jar" and "jms.jar" files. Building the example -------------------- * Set the JAVA_HOME environment variable to point to the JDK. * Simply run the Jakarta ant build tool to build the applet. The applet compilation process is a bit different from the typical standalone Message Queue clients. It needs to do the following things - * Compile MQApplet.java into MQApplet.class * Bundle MQApplet.class into a jar file -> mqapplet.jar * Copy the imq.jar and jms.jar files to a local directory. * Generate a self signed certificate using keytool. * Sign mqapplet.jar, imq.jar and jms.jar using this certificate.   Applets must be signed if they need to do anything non-trivial.   Otherwise you will get security exceptions at runtime. Running the example applet -------------------------- The build script places all the files necessary for running this applet into a new directory named "install". Copy all the contents of this directory somewhere under a web server's document root. This includes -     * mqapplet.html     * Signed Jar files : mqapplet.jar, imq.jar, jms.jar After this, the applet can be run by simply pointing the browser to the "mqapplet.html" file. After the applet is loaded, the web browser (Java plugin) should open a dialog box asking for permission to run a signed applet. Click on the "Yes" button. When the applet starts running it will not automatically connect to a Message Queue broker. Please specify the broker's address and click  on the "Connect" button.  Running as a standalone application ----------------------------------- The MQApplet.class(build/MQApplet.class) can also be used as a  standalone application. It presents the same UI as the applet. To run  this example as a standalone application, you need to set CLASSPATH  to include at least:     jms.jar     imq.jar     directory containing this example A detailed guideline on setting CLASSPATH is found in the README file in the jms demo subdirectory as well as in the "Quick Start Tutorial" in the Sun Java(tm) System Message Queue Developer's Guide. The following are examples for setting CLASSPATH on the different platforms. These commands are run from the directory containing this example. On Solaris:     setenv CLASSPATH /usr/share/lib/jms.jar:/usr/share/lib/imq.jar:. On Windows:     set CLASSPATH=%IMQ_HOME%\lib\jms.jar;%IMQ_HOME%\lib\imq.jar;. On Linux:     setenv CLASSPATH /opt/sun/mq/share/lib/jms.jar:   /opt/sun/mq/share/lib/imq.jar:. #####hpux-dev##### On HP-UX:     export CLASSPATH=/opt/sun/mq/share/lib/jms.jar:/opt/sun/mq/share/lib/imq.jar:. Note that it is assumed that the above export command is run on BASH shell After setting the CLASSPATH, simply run -     java MQApplet Obviously, you don't need the signed jar files for running as a standalone application. /*  * @(#)MQApplet.java  1.1 10/10/03  *  * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.  *  * Sun grants you ("Licensee") a non-exclusive, royalty free, license  * to use, modify and redistribute this software in source and binary  * code form, provided that i) this copyright notice and license  * appear on all copies of the software; and ii) Licensee does not  * utilize the software in a manner which is disparaging to Sun.  *  * This software is provided "AS IS," without a warranty of any kind.  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY  * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE  * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS  * BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,  * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING  * OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.  *  * This software is not designed or intended for use in on-line  * control of aircraft, air traffic, aircraft navigation or aircraft  * communications; or in the design, construction, operation or  * maintenance of any nuclear facility. Licensee represents and  * warrants that it will not use or redistribute the Software for such  * purposes.  */ import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; import javax.swing.event.*; import javax.jms.*; /**  * This is a simple chat applet that uses JMS APIs. It uses publish  * subscribe model. It can also be run as a standalone application.  */ public class MQApplet extends JApplet     implements ActionListener, ExceptionListener, MessageListener {     private static boolean DEBUG = Boolean.getBoolean("mqapplet.debug");     private static String TRANSPORT;     static {         TRANSPORT = System.getProperty("mqapplet.transport");         if (TRANSPORT == null)             TRANSPORT = "";     }     private JPanel mainPanel;     private StatusPanel statusBar;     private JTextField urlField;     private JTextField hostField, portField;     private JTextField addrField;     private JTextArea txArea, rxArea;     JButton connectButton, exitButton, clearButton, sendButton;     private boolean createExitButton = false;     public void init() {         initGUI();         initJMS();     }     public void destroy() {         shutdownJMS();         shutdownGUI();     }     private void enableExit() {         createExitButton = true;     }     private void initGUI() {         // The application window contains the 'mainPanel' container         // and a status bar.         Container content = getContentPane();         // Create the mainPanel container. It holds all the UI         // components...         mainPanel = new JPanel();         mainPanel.setLayout(new BorderLayout());         content.add("Center", mainPanel);         // Create the status bar..         statusBar = new StatusPanel();         content.add("South", statusBar);         //         // Now start populating mainPanel...         //         // dialogPanel contains JMS configuration and the connect         // button.         JPanel dialogPanel = new JPanel();         dialogPanel.setLayout(             new BoxLayout(dialogPanel, BoxLayout.Y_AXIS));         dialogPanel.setBorder(             createMyBorder("JMS Connection Properties..."));         JPanel dummyPanel;         if (TRANSPORT.equalsIgnoreCase("http")) {             dummyPanel = new JPanel();             dummyPanel.setLayout(new BoxLayout(dummyPanel, BoxLayout.X_AXIS));             dummyPanel.add(new JLabel("imqConnectionURL : "));             urlField = new JTextField("http://");             dummyPanel.add(urlField);             dialogPanel.add(dummyPanel);         }         else if (TRANSPORT.equalsIgnoreCase("tcp")) {             dummyPanel = new JPanel();             dummyPanel.setLayout(new BoxLayout(dummyPanel, BoxLayout.X_AXIS));             dummyPanel.add(new JLabel("imqBrokerHostName : "));             hostField = new JTextField("localhost");             dummyPanel.add(hostField);             dialogPanel.add(dummyPanel);             dummyPanel = new JPanel();             dummyPanel.setLayout(new BoxLayout(dummyPanel, BoxLayout.X_AXIS));             dummyPanel.add(new JLabel("imqBrokerHostPort : "));             portField = new JTextField("7676");             dummyPanel.add(portField);             dialogPanel.add(dummyPanel);         }         else {             dummyPanel = new JPanel();             dummyPanel.setLayout(new BoxLayout(dummyPanel, BoxLayout.X_AXIS));             dummyPanel.add(new JLabel("imqAddressList : "));             addrField = new JTextField("mq://localhost:7676");             dummyPanel.add(addrField);             dialogPanel.add(dummyPanel);         }         dummyPanel = new JPanel();         dummyPanel.setLayout(new BoxLayout(dummyPanel, BoxLayout.X_AXIS));         connectButton = new JButton();         connectButton.setBorder(BorderFactory.createCompoundBorder(             BorderFactory.createEmptyBorder(3, 0, 3, 3),             connectButton.getBorder()));         connectButton.addActionListener(this);         setConnectButton("Connect");         dummyPanel.add(connectButton);         if (createExitButton) {             exitButton = new JButton("Exit");             exitButton.setBorder(BorderFactory.createCompoundBorder(                 BorderFactory.createEmptyBorder(3, 3, 3, 3),                 exitButton.getBorder()));             exitButton.addActionListener(this);             dummyPanel.add(exitButton);         }         dialogPanel.add(dummyPanel);         JPanel messagePanel = new JPanel();         messagePanel.setLayout(new GridLayout(2, 1));         dummyPanel = new JPanel();         dummyPanel.setLayout(new BoxLayout(dummyPanel, BoxLayout.Y_AXIS));         dummyPanel.setBorder(createMyBorder("Received messages "));         rxArea = new JTextArea();         rxArea.setEditable(false);         JScrollPane spane = new JScrollPane(rxArea,             JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,             JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);         dummyPanel.add(spane);         clearButton = new JButton("Clear");         clearButton.setBorder(BorderFactory.createCompoundBorder(             BorderFactory.createEmptyBorder(3, 3, 3, 3),             clearButton.getBorder()));         clearButton.addActionListener(this);         dummyPanel.add(clearButton);         messagePanel.add(dummyPanel);         dummyPanel = new JPanel();         dummyPanel.setLayout(new BoxLayout(dummyPanel, BoxLayout.Y_AXIS));         dummyPanel.setBorder(createMyBorder("Send message "));         txArea = new JTextArea();         txArea.setEditable(true);         spane = new JScrollPane(txArea,             JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,             JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);         dummyPanel.add(spane);         sendButton = new JButton("Send");         sendButton.setBorder(BorderFactory.createCompoundBorder(             BorderFactory.createEmptyBorder(3, 3, 3, 3),             sendButton.getBorder()));         sendButton.addActionListener(this);         dummyPanel.add(sendButton);         messagePanel.add(dummyPanel);         mainPanel.add("North", dialogPanel);         mainPanel.add("Center", messagePanel);     }     private void initJMS() {     }     private void shutdownGUI() {         remove(mainPanel);         mainPanel = null;     }     private void shutdownJMS() {         doDisconnect();     }     public void processEvent(AWTEvent e) {         if (e.getID() == Event.WINDOW_DESTROY) {             System.exit(0);         }     }     public void actionPerformed(ActionEvent e) {         if (e.getActionCommand().equals("Connect")) {             if (TRANSPORT.equalsIgnoreCase("http")) {                 String us = urlField.getText();                 statusBar.setStatusLine("Connecting to " + us + "...");                 initHTTPConnectionFactory(us);             }             else if (TRANSPORT.equalsIgnoreCase("tcp")) {                 String h = hostField.getText();                 String p = portField.getText();                 statusBar.setStatusLine("Connecting to " + h + ":" + p + "...");                 initTCPConnectionFactory(h, p);             }             else {                 String addr = addrField.getText();                 statusBar.setStatusLine("Connecting to " + addr + "...");                 initConnectionFactory(addr);             }             doConnect();         }         if (e.getActionCommand().equals("Disconnect")) {             statusBar.setStatusLine("Disconnecting...");             doDisconnect();             statusBar.setStatusLine("Connection closed.");         }         if (e.getActionCommand().equals("Send")) {             String ss = txArea.getText();             doSend(ss);             txArea.setText(null);         }         if (e.getActionCommand().equals("Clear")) {             rxArea.setText(null);         }         if (e.getActionCommand().equals("Exit")) {             doDisconnect();             System.exit(0);         }     }     public void updateRxArea(String s) {         rxArea.append(s);     }     public void enableConnectButton() {         setConnectButton("Connect");     }     public void enableDisconnectButton() {         setConnectButton("Disconnect");     }     ConnectionFactory connectionFactory = null;     Connection connection = null;     Session session = null;     Topic topic = null;     MessageConsumer msgConsumer = null;     MessageProducer msgProducer = null;     TextMessage textMessage = null;     public void initHTTPConnectionFactory(String s) {         try {             if (connectionFactory == null) {                 connectionFactory = (ConnectionFactory)                     new com.sun.messaging.ConnectionFactory();             }             // Provider specific code start.             com.sun.messaging.ConnectionFactory cf =                 (com.sun.messaging.ConnectionFactory) connectionFactory;             cf.setProperty(                 com.sun.messaging.ConnectionConfiguration.imqConnectionType,                 "HTTP");             cf.setProperty(                 com.sun.messaging.ConnectionConfiguration.imqConnectionURL,                 s);             // Provider specific code end.         }         catch (JMSException e) {             updateRxArea("initHTTPConnectionFactory : " + e.toString() + "\n");             e.printStackTrace();             if (e.getLinkedException() != null)                 e.getLinkedException().printStackTrace();         }     }     public void initTCPConnectionFactory(String h, String p) {         try {             if (connectionFactory == null) {                 connectionFactory = (ConnectionFactory)                     new com.sun.messaging.ConnectionFactory();             }             // Provider specific code start.             com.sun.messaging.ConnectionFactory cf =                 (com.sun.messaging.ConnectionFactory) connectionFactory;       // Set imqAddressList property.             ((com.sun.messaging.ConnectionFactory)cf).setProperty(                 com.sun.messaging.ConnectionConfiguration.imqAddressList,                 new StringBuffer().append("mq://").append(h).append(                     ":").append(p).append("/jms").toString());             // Provider specific code end.         }         catch (JMSException e) {             updateRxArea("initTCPConnectionFactory : " + e.toString() + "\n");             e.printStackTrace();             if (e.getLinkedException() != null)                 e.getLinkedException().printStackTrace();         }     }     public void initConnectionFactory(String a) {         if (connectionFactory == null) {             connectionFactory = (ConnectionFactory)                 new com.sun.messaging.ConnectionFactory();         }         try {             // Provider specific code start.             com.sun.messaging.ConnectionFactory cf =                 (com.sun.messaging.ConnectionFactory) connectionFactory;             cf.setProperty(                 com.sun.messaging.ConnectionConfiguration.imqAddressList,                 a);             // Provider specific code end.         }         catch (JMSException e) {             updateRxArea("initConnectionFactory : " + e.toString() + "\n");             e.printStackTrace();             if (e.getLinkedException() != null)                 e.getLinkedException().printStackTrace();         }     }     public void doConnect() {         try {             connection = connectionFactory.createConnection();             connection.setExceptionListener(this);             connection.start();             session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);             topic = session.createTopic("MQChatAppletTopic");             msgConsumer = session.createConsumer(topic);             msgConsumer.setMessageListener(this);             msgProducer = session.createProducer(topic);             textMessage = session.createTextMessage();             statusBar.setStatusLine("Connected");             enableDisconnectButton();         }         catch (JMSException e) {             updateRxArea("doConnect : " + e.toString() + "\n");             statusBar.setStatusLine("Unable to connect.");             e.printStackTrace();             if (e.getLinkedException() != null)                 e.getLinkedException().printStackTrace();         }     }     public void doSend(String s) {         if (msgProducer == null) {             statusBar.setStatusLine("Not connected.");             return;         }         try {             textMessage.setText(s);             msgProducer.send(textMessage);         }         catch (JMSException e) {             updateRxArea("doSend : " + e.toString() + "\n");             e.printStackTrace();         }     }     public void doDisconnect() {         try {             if (connection != null)                 connection.close();         }         catch (Exception e) {}         connection = null;         session = null;         topic = null;         msgConsumer = null;         msgProducer = null;         textMessage = null;         enableConnectButton();     }     public void onException(JMSException e) {         statusBar.setStatusLine("Connection lost : " + e.toString());         doDisconnect();     }     public void onMessage(Message m) {         try {             if (m instanceof TextMessage) {                 String s = ((TextMessage) m).getText();                 updateRxArea(s);             }         }         catch (JMSException e) {             e.printStackTrace();             updateRxArea("onMessage : " + e.toString() + "\n");         }     }     private void setConnectButton(String text) {         connectButton.setText(text);         connectButton.setActionCommand(text);         connectButton.invalidate();         connectButton.validate();         mainPanel.repaint();     }     private javax.swing.border.Border createMyBorder(String title) {         javax.swing.border.Border inner =             BorderFactory.createLineBorder(Color.black);         if (title != null)             inner = BorderFactory.createTitledBorder(inner, title);         javax.swing.border.Border outer =             BorderFactory.createEmptyBorder(3, 3, 3, 3);         return BorderFactory.createCompoundBorder(outer, inner);     }     public void cleanupAndExit() {         destroy();         System.exit(0);     }     public static MQApplet mq = null;     public static void mainWindowClosed() {         mq.cleanupAndExit();     }     public static void main(String []args) {         JFrame f = new JFrame("MQApplet");         f.setDefaultCloseOperation(f.DO_NOTHING_ON_CLOSE);         f.addWindowListener(new WindowAdapter() {             public void windowClosing(WindowEvent e) {                 mainWindowClosed();             }         });         mq = new MQApplet();         mq.enableExit();         mq.init();         mq.start();         f.getContentPane().add("Center", mq);         f.setSize(600, 600);         f.show();     }     class StatusPanel extends JPanel {         private JLabel label = null;         public StatusPanel() {             setLayout(new BorderLayout());             setBorder(BorderFactory.createLoweredBevelBorder());             label = new JLabel();             int size = label.getFont().getSize();             label.setFont(new Font("Serif", Font.PLAIN, size));             add("West", label);             setStatusLine("Ready");         }         public void setStatusLine(String statusLine) {             if (statusLine == null)                 statusLine = "";             label.setText(statusLine);             invalidate();             validate();             repaint();         }     } } /*  * EOF  */          mqapplet.zip( 7 k)