Source code: org/mandarax/eca/example/MessageSender.java
1 /*
2 * Copyright (C) 1998-2002 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package org.mandarax.eca.example;
20
21 import java.awt.*;
22 import java.awt.event.ActionEvent;
23
24 import javax.jms.*;
25 import javax.naming.*;
26 import javax.swing.*;
27
28 /**
29 * Dummy sending (JMS) messages.
30 */
31 public class MessageSender extends JFrame {
32 public static final String QUEUE_NAME = "queue 1";
33 // components
34 private JTextArea txtArea = new JTextArea();
35 private Action actExit = null;
36 private Action actSend = null;
37 private Action actCopy = null;
38 private Action actPaste = null;
39 // JMS objects
40 Context jndiContext = null;
41 QueueConnectionFactory queueConnectionFactory = null;
42 QueueConnection queueConnection = null;
43 QueueSession queueSession = null;
44 Queue queue = null;
45 QueueSender queueSender = null;
46 /**
47 * Lauch the sender.
48 */
49 public static void main(String[] args) {
50 MessageSender sender = new MessageSender();
51 sender.setLocation(30, 30);
52 sender.setSize(300, 200);
53 sender.show();
54 }
55 /**
56 * Constructor.
57 */
58 public MessageSender() {
59 super("SEND MESSAGE VIA JMS");
60 initialize();
61 }
62 /**
63 * Set up the instance.
64 */
65 private void initialize() {
66
67 JPanel mainPane = (JPanel) this.getContentPane();
68 mainPane.setLayout(new BorderLayout(5, 5));
69 mainPane.add(new JScrollPane(txtArea), BorderLayout.CENTER);
70
71 // initialize actions
72 actExit = new AbstractAction("exit") {
73 public void actionPerformed(ActionEvent e) {
74 dispose();
75 }
76 };
77 actSend = new AbstractAction("send") {
78 public void actionPerformed(ActionEvent e) {
79 send();
80 }
81 };
82 actCopy = new AbstractAction("copy") {
83 public void actionPerformed(ActionEvent e) {
84 txtArea.copy();
85 }
86 };
87 actPaste = new AbstractAction("paste") {
88 public void actionPerformed(ActionEvent e) {
89 txtArea.paste();
90 }
91 };
92 // toolbar
93 JToolBar toolbar = new JToolBar();
94 toolbar.setFloatable(false);
95 toolbar.add(actExit);
96 toolbar.addSeparator();
97 toolbar.add(actCopy);
98 toolbar.add(actPaste);
99 toolbar.addSeparator();
100 toolbar.add(actSend);
101 mainPane.add(toolbar, BorderLayout.NORTH);
102
103 // queue label
104 mainPane.add(new JLabel("Send messages to queue: \"" + QUEUE_NAME + "\"",JLabel.CENTER),BorderLayout.SOUTH);
105
106 }
107 /**
108 * Handle an exception.
109 * @param x an exception
110 * @param msg an exception message
111 */
112 private void handleException(Exception x,String msg) {
113 x.printStackTrace();
114 JOptionPane.showMessageDialog(this,msg,"Exception",JOptionPane.WARNING_MESSAGE);
115 }
116 /**
117 * Send the message,
118 */
119 private void send() {
120
121 TextMessage message = null;
122
123 try {
124 jndiContext = new InitialContext();
125 }
126 catch (NamingException e) {
127 handleException(e,"Could not create JNDI API");
128 }
129 // Look up connection factory and queue.
130 try {
131 queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory");
132 queue = (Queue) jndiContext.lookup(QUEUE_NAME);
133 }
134 catch (NamingException e) {
135 handleException(e,"JNDI API lookup failed");
136 }
137 try {
138 queueConnection = queueConnectionFactory.createQueueConnection();
139 queueSession = queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
140 queueSender = queueSession.createSender(queue);
141 message = queueSession.createTextMessage();
142
143 message.setText(txtArea.getText());
144 queueSender.send(message);
145 queueSender.send(queueSession.createMessage());
146 } catch (JMSException e) {
147 handleException(e,"Exception occurred when sending message");
148 } finally {
149 if (queueConnection != null) {
150 try {
151 queueConnection.close();
152 } catch (JMSException e) {
153 handleException(e,"Exception occurred when closing connection");
154 }
155 }
156
157 }
158 }
159 }