1 23 import javax.microedition.midlet.*; 24 import javax.microedition.lcdui.*; 25 import javax.microedition.io.*; 26 27 import com.scalagent.kjoram.*; 28 import com.scalagent.kjndi.ksoap.*; 29 30 import java.io.*; 31 import java.util.*; 32 33 35 public class KConsumer extends MIDlet implements CommandListener { 36 37 Display display; 38 Form mainScreen; 39 int max = 5; 40 41 Topic topic; 42 Queue queue; 43 MsgListener listener; 44 45 public void addText(String text) { 46 if (max < mainScreen.size()) 47 mainScreen.delete(0); 48 mainScreen.append(text); 49 display.setCurrent(mainScreen); 50 } 51 public void commandAction(Command cmd, Displayable d) { 52 53 } 54 55 protected void startApp() throws MIDletStateChangeException { 56 display = Display.getDisplay(this); 57 mainScreen = new Form("Consumes messages "); 58 addText("Consumes messages."); 59 display.setCurrent(mainScreen); 60 61 try { 62 SoapNamingContext ictx = new SoapNamingContext("X.X.X.X",8080); 63 System.out.println("ictx=" + ictx); 64 queue = (Queue) ictx.lookup("queue"); 65 System.out.println("queue=" + queue); 66 topic = (Topic) ictx.lookup("topic"); 67 System.out.println("topic=" + topic); 68 69 listener = new MsgListener(this); 70 71 Thread t = new Thread () { 72 public void run() { 73 subscribeTopic(listener,topic); 74 } 75 }; 76 t.start(); 77 78 Thread t1 = new Thread () { 79 public void run() { 80 subscribeQueue(queue); 81 } 82 }; 83 t1.start(); 84 85 display.setCurrent(mainScreen); 87 } catch (Exception exc) { 88 System.out.println("////////// EXCEPTION " + exc); 89 exc.printStackTrace(); 90 } 91 } 92 93 void subscribeTopic(MessageListener listener, Topic topic) { 94 System.out.println("KConsumer.subscribeTopic(" + topic + ")"); 95 try { 96 ConnectionFactory cf = 97 new com.scalagent.kjoram.ksoap.SoapConnectionFactory( 98 "X.X.X.X", 8080, 360000); 99 com.scalagent.kjoram.Connection cnx = cf.createConnection(); 100 Session sess = cnx.createSession(false, Session.AUTO_ACKNOWLEDGE); 101 MessageConsumer tConsumer = sess.createConsumer(topic); 102 tConsumer.setMessageListener(listener); 103 cnx.start(); 104 mainScreen.setCommandListener(this); 105 } catch (Exception exc) { 106 System.out.println("subscribeTopic ::::: EXCEPTION"); 107 exc.printStackTrace(); 108 } 109 } 110 111 void subscribeQueue(Queue queue) { 112 System.out.println("KConsumer.subscribeQueue(" + queue + ")"); 113 try { 114 ConnectionFactory cf = 115 new com.scalagent.kjoram.ksoap.SoapConnectionFactory( 116 "X.X.X.X", 8080, 360000); 117 com.scalagent.kjoram.Connection cnx = cf.createConnection(); 118 Session sess = cnx.createSession(false, Session.AUTO_ACKNOWLEDGE); 119 MessageConsumer qConsumer = sess.createConsumer(queue); 120 TextMessage msg; 121 cnx.start(); 122 for (int i = 0; i < 100; i++) { 123 msg = (TextMessage) qConsumer.receive(); 124 addText("\nfrom queue: " + msg.getText()); 126 display.setCurrent(mainScreen); 127 } 128 mainScreen.setCommandListener(this); 129 } catch (Exception exc) { 130 System.out.println("subscribeQueue ::::: EXCEPTION"); 131 exc.printStackTrace(); 132 } 133 } 134 135 protected void pauseApp() { 136 } 137 138 protected void destroyApp(boolean arg0) throws MIDletStateChangeException { 139 } 140 } 141 142 143 | Popular Tags |