1 /*------------------------------------------------------------------------------
 2  Name:      xmlBlaster/demo/javaclients/email/DemoCb.java
 3  Project:   xmlBlaster.org
 4  Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
 5  ------------------------------------------------------------------------------*/
 6 package javaclients.email;
 7 
 8 import org.xmlBlaster.util.Global;
 9 import org.xmlBlaster.client.key.UpdateKey;
10 import org.xmlBlaster.client.qos.ConnectQos;
11 import org.xmlBlaster.client.qos.UpdateQos;
12 import org.xmlBlaster.client.qos.UpdateReturnQos;
13 import org.xmlBlaster.client.I_Callback;
14 import org.xmlBlaster.client.I_XmlBlasterAccess;
15 import org.xmlBlaster.util.MsgUnit;
16 //import org.xmlBlaster.util.qos.address.CallbackAddress;
17 import java.util.logging.Logger;
18 
19 /**
20  * This client connects to xmlBlaster and subscribes to a message. We then
21  * publish the message and receive it asynchronous in the update() method.
22  * <p />
23  * Invoke:
24  * <pre>
25  *   java org.xmlBlaster.Main
26  *   java javaclients.email.DemoCb -protocol email -mail.from demo@localhost
27  * </pre>
28  * 
29  * @see <a
30  *      href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/protocol.email.html">The
31  *      protocol.email requirement</a>
32  * @author <a href="mailto:xmlBlaster@marcelruff.info">Marcel Ruff</a>
33  */
34 public class DemoCb implements I_Callback {
35    private static Logger log = Logger.getLogger(DemoCb.class.getName());
36 
37    private Global glob;
38 
39    public DemoCb(Global glob) {
40       this.glob = glob;
41       try {
42          I_XmlBlasterAccess con = glob.getXmlBlasterAccess();
43 
44          ConnectQos qos = new ConnectQos(glob);
45 
46          //CallbackAddress cbAddr = new CallbackAddress(glob, "EMAIL");
47          //cbAddr.setRawAddress("demo@localhost");
48          //qos.addCallbackAddress(cbAddr);
49          
50          con.connect(qos, this);
51 
52          con.subscribe("<key oid='EmailDemo'/>", "<qos/>");
53 
54          while (true) {
55             int ch = Global.waitOnKeyboardHit("Hit a key to publish a message (type 'q' to quit)>");
56             if (ch == 'q')
57                break;
58             con.publish(new MsgUnit("<key oid='EmailDemo'/>", "Hi".getBytes(),
59                   "<qos/>"));
60             Thread.sleep(1000);
61          }
62          con.disconnect(null);
63       } catch (Exception e) {
64          System.out.println(e.toString());
65       }
66    }
67 
68    public String update(String cbSessionId, UpdateKey updateKey,
69          byte[] content, UpdateQos updateQos) {
70       if (updateKey.isInternal()) {
71          log.info("Received unexpected internal message '" + updateKey.getOid()
72                + " from xmlBlaster");
73          return "";
74       }
75       log.info("Received asynchronous message '" + updateKey.getOid()
76             + "' state=" + updateQos.getState() + " content='"
77             + new String(content) + "' from xmlBlaster");
78 
79       UpdateReturnQos uq = new UpdateReturnQos(glob);
80       return uq.toXml();
81    }
82 
83    public static void main(String args[]) {
84       Global glob = new Global(); // initializes args, properties etc.
85       if (glob.init(args) < 0) {
86          System.out.println("EmailDemo wrong args, Bye");
87          System.exit(1);
88       }
89 
90       new DemoCb(glob);
91    }
92 }


syntax highlighted by Code2HTML, v. 0.9.1