KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > KConsumer


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Nicolas Tachker (ScalAgent)
21  * Contributor(s):
22  */

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 /**
34  */

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 JavaDoc 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 JavaDoc t = new Thread JavaDoc() {
72           public void run() {
73             subscribeTopic(listener,topic);
74           }
75         };
76       t.start();
77
78       Thread JavaDoc t1 = new Thread JavaDoc() {
79           public void run() {
80             subscribeQueue(queue);
81           }
82         };
83       t1.start();
84       
85       // activiate the screen
86
display.setCurrent(mainScreen);
87     } catch (Exception JavaDoc 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 JavaDoc 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         //System.out.println("### from queue: " + msg.getText());
125
addText("\nfrom queue: " + msg.getText());
126         display.setCurrent(mainScreen);
127       }
128       mainScreen.setCommandListener(this);
129     } catch (Exception JavaDoc 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