1 package sample.jms.topics.GuiChat; 2 3 52 53 59 import javax.jms.*; 60 import org.mr.api.jms.MantaConnectionFactory; 61 62 public class GuiChat implements javax.jms.MessageListener 63 { 64 private static final int MESSAGE_TTL = 6000000; 65 private Connection connect = null; 67 private Session pubSession = null; 68 private Session subSession = null; 69 private MessageProducer publisher = null; 70 GuiContiner GuiDisplay=null; 72 73 public GuiChat(GuiContiner gui){ 75 GuiDisplay=gui; 77 try 79 { 80 ConnectionFactory factory; 81 factory = new MantaConnectionFactory(); 82 connect = factory.createConnection(); 83 pubSession = connect.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE); 84 subSession = connect.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE); 85 } 86 catch (javax.jms.JMSException jmse) 87 { 88 System.err.println("error while creating connection - " + jmse.toString()); 89 jmse.printStackTrace(); 90 System.exit(1); 91 } 92 createRoom("MAIN_ROOM"); 94 } 96 100 public void createRoom(String chatTopic) { 101 try 103 { 104 javax.jms.Topic topic = pubSession.createTopic(chatTopic); 105 javax.jms.MessageConsumer subscriber = subSession.createConsumer(topic); 106 subscriber.setMessageListener(this); 107 publisher = pubSession.createProducer(topic); 108 connect.start(); 110 } 111 catch (javax.jms.JMSException jmse) 112 { 113 jmse.printStackTrace(); 114 } 115 } 117 120 public void chatter(String username, String message) 121 { 122 try{ 123 javax.jms.TextMessage msg = pubSession.createTextMessage(); 125 msg.setText(username+"> "+message); 126 publisher.send( msg, 128 javax.jms.DeliveryMode.NON_PERSISTENT, 129 javax.jms.Message.DEFAULT_PRIORITY, 130 MESSAGE_TTL); 131 } 132 catch ( javax.jms.JMSException jmse ) 133 { 134 jmse.printStackTrace(); 135 } 136 } 138 142 public void onMessage( javax.jms.Message aMessage) 143 { 144 try 145 { 146 javax.jms.TextMessage textMessage = (javax.jms.TextMessage ) aMessage; 148 try 150 { 151 String message = textMessage.getText(); 152 GuiDisplay.doChat(message); 153 } 154 catch (javax.jms.JMSException jmse) 155 { 156 jmse.printStackTrace(); 157 } 158 } 159 catch (java.lang.RuntimeException rte) 160 { 161 rte.printStackTrace(); 162 } 163 } 165 } 167 | Popular Tags |