1 package sample.jms.StockExample; 2 3 52 53 65 import javax.swing.*; 66 67 import java.awt.*; 68 import java.awt.event.*; 69 import java.util.HashMap ; 70 71 import org.mr.api.jms.MantaConnectionFactory; 72 import javax.jms.ConnectionFactory ; 73 import javax.jms.JMSException ; 74 import javax.jms.Message ; 75 import javax.jms.MessageConsumer ; 76 import javax.jms.MessageProducer ; 77 import javax.jms.Session ; 78 79 public class StockPublisherExample implements ActionListener, javax.jms.MessageListener { 80 81 public static String companyValueSeperator = "}"; 82 static StockStatus stockStatus=null; 83 public static ConnectionFactory conFactory=null; 84 public HashMap stocksMap=null; 86 JFrame stockFrame; 88 JPanel stockPanel; 89 JTextField companyName; 90 JLabel instructions; 91 JButton makeQuery; 92 StocksTable model = new StocksTable(); 93 94 private static final int MESSAGE_TTL = 0; 96 private javax.jms.Connection con = null; 97 private javax.jms.Session pubSession = null; 98 private javax.jms.Session subSession = null; 99 private javax.jms.MessageProducer qSender = null; 100 private javax.jms.Topic stockConsumer=null; 101 private javax.jms.Queue queryProducer=null; 102 103 107 public StockPublisherExample() { 108 stockFrame = new JFrame("Present Stock Values"); 110 JFrame.setDefaultLookAndFeelDecorated(true); 111 stockFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 112 companyName = new JTextField(10); 114 makeQuery = new JButton("Query"); 115 instructions = new JLabel("Enter company name then press Query", SwingConstants.LEFT); 116 makeQuery.addActionListener(this); 118 JTable table = new JTable(model); 120 table.setPreferredScrollableViewportSize(new Dimension(250, 50)); 121 122 JScrollPane scrollPane = new JScrollPane(table); 124 stockPanel = (JPanel)stockFrame.getContentPane(); 125 stockPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); 126 Box top = new Box(BoxLayout.Y_AXIS); 127 Box instructionsPanel = new Box(BoxLayout.X_AXIS); 128 instructionsPanel.add(instructions); 129 instructionsPanel.add(Box.createHorizontalGlue()); 130 top.add(instructionsPanel); 131 Box queryPanel = new Box(BoxLayout.X_AXIS); 132 queryPanel.add(companyName); 133 queryPanel.add(makeQuery); 134 top.add(queryPanel); 135 stockPanel.add(top, BorderLayout.NORTH); 136 stockPanel.add(scrollPane, BorderLayout.CENTER); 137 138 stockFrame.pack(); 140 stockFrame.setVisible(true); 141 } 142 143 147 public void actionPerformed(ActionEvent event) { 148 String name=companyName.getText(); 149 this.sendQuery(name); 150 151 } 153 157 private static void createAndShowGUI() { 158 JFrame.setDefaultLookAndFeelDecorated(true); 160 StockPublisherExample converter = new StockPublisherExample(); 161 converter.createDestinations(); 162 } 164 168 public void createDestinations(){ 169 try 171 { 172 con = StockPublisherExample.conFactory.createConnection(); 174 pubSession = con.createSession(false,Session.AUTO_ACKNOWLEDGE); 176 subSession = con.createSession(false,Session.AUTO_ACKNOWLEDGE); 177 } 178 catch (javax.jms.JMSException jmse) 179 { 180 jmse.printStackTrace(); 181 System.exit(1); 182 } 183 try { 184 stockConsumer = subSession.createTopic("SampleTopic1"); 186 MessageConsumer consumer = subSession.createConsumer(stockConsumer); 187 consumer.setMessageListener(this); 188 queryProducer = pubSession.createQueue("q1"); 190 qSender = (MessageProducer )pubSession.createProducer(queryProducer); 191 con.start(); 192 } catch (JMSException e) { 193 e.printStackTrace(); 194 } } 197 200 public void sendQuery(String company){ 201 try{ 202 if(company==null||company.equals("")) 203 return; 204 javax.jms.TextMessage msg = this.pubSession.createTextMessage(); 206 msg.setText(company); 207 qSender.send( msg, 210 javax.jms.DeliveryMode.NON_PERSISTENT, 211 javax.jms.Message.DEFAULT_PRIORITY, 212 MESSAGE_TTL); 213 } 214 catch ( javax.jms.JMSException jmse ) 215 { 216 jmse.printStackTrace(); 217 } 218 } 220 223 public void onMessage(Message message) { 224 javax.jms.TextMessage stockValuesMessage = (javax.jms.TextMessage ) message; 226 String stockInfo=null; 227 try 229 { 230 stockInfo = stockValuesMessage.getText(); 231 }catch (javax.jms.JMSException jmse){ 232 jmse.printStackTrace(); 233 } 234 String [] newValues=stockInfo.split(companyValueSeperator); 236 model.updateCompany(newValues[0],newValues[1]); 238 } 240 public static void main(String [] args) { 241 System.out.println("MANTARAY STOCK PUBLISHER EXAMPLE IS STARTING"); 242 conFactory = new MantaConnectionFactory(); 243 stockStatus = new StockStatus(); 245 stockStatus.createDestinations(); 246 javax.swing.SwingUtilities.invokeLater(new Runnable () { 248 public void run() { 249 createAndShowGUI(); 250 } 251 }); 252 stockStatus.start(); 253 } 255 } | Popular Tags |