1 package org.xmlBlaster.test.memoryleak;
2
3 // xmlBlaster/demo/javaclients/PublishErase.java
4 import java.util.logging.Logger;
5 import java.util.logging.Level;
6 import org.xmlBlaster.util.Global;
7 import org.xmlBlaster.client.qos.DisconnectQos;
8 import org.xmlBlaster.util.XmlBlasterException;
9 import org.xmlBlaster.client.I_Callback;
10 import org.xmlBlaster.client.key.EraseKey;
11 import org.xmlBlaster.client.key.UpdateKey;
12 import org.xmlBlaster.client.qos.ConnectQos;
13 import org.xmlBlaster.client.qos.ConnectReturnQos;
14 import org.xmlBlaster.client.qos.PublishQos;
15 import org.xmlBlaster.client.qos.UpdateQos;
16 import org.xmlBlaster.client.qos.EraseQos;
17 import org.xmlBlaster.client.qos.EraseReturnQos;
18 import org.xmlBlaster.client.I_XmlBlasterAccess;
19 import org.xmlBlaster.util.MsgUnit;
20
21 import java.io.*;
22
23 /**
24 * Creating/destroying topic in bulks of 100.
25 */
26 public class PublishErase
27 {
28 private final String ME = "PublishErase";
29 private static Logger log = Logger.getLogger(PublishErase.class.getName());
30 private I_XmlBlasterAccess con = null;
31 private ConnectReturnQos conRetQos = null;
32 private boolean connected;
33 private int bulkSize = 100;
34
35 public PublishErase(final Global glob) {
36
37
38 bulkSize = glob.getProperty().get("bulkSize", bulkSize);
39
40 try {
41 con = glob.getXmlBlasterAccess();
42 ConnectQos qos = new ConnectQos(glob);
43 conRetQos = con.connect(qos, new I_Callback() {
44
45 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
46 log.info("Receiving asynchronous message '" + updateKey.getOid() +
47 "' state=" + updateQos.getState() + " in default handler");
48 return "";
49 }
50
51 }); // Login to xmlBlaster, default handler for updates
52
53 String xmlKey = null;
54 PublishQos qw = new PublishQos(glob);
55 EraseQos eq = new EraseQos(glob);
56 System.out.println("qos = " + qw.toXml() );
57 byte[] b = new byte[1024];
58 long lCount = 0L;
59 while(true) {
60 lCount++;
61 xmlKey = "<key oid='" + lCount +
62 "'> <topic id='aaaa'/>" +
63 "</key>";
64 con.publish(new MsgUnit(xmlKey,b,qw.toXml()));
65
66 try { Thread.sleep(5L); } catch( InterruptedException i) {}
67
68 EraseKey ek = new EraseKey(glob, "" + lCount);
69 EraseReturnQos[] er = con.erase(ek.toXml(), eq.toXml());
70
71 // System.out.println(new Timestamp(System.currentTimeMillis())+":"+lCount);
72 if ((lCount % bulkSize) == 0) {
73 log.info("Published and erased " + lCount + " topics, enter return to continue, enter 'q' to quit");
74 try {
75 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
76 String line = in.readLine(); // Blocking in I/O
77 if (line == null) continue;
78 line = line.trim();
79 if (line.toLowerCase().equals("q")) {
80 break;
81 }
82 }
83 catch(Exception e) {
84 log.severe(e.toString());
85 break;
86 }
87 }
88 }
89 }
90 catch (XmlBlasterException e) {
91 log.severe("Houston, we have a problem: " + e.toString());
92 }
93 finally {
94 log.info("Success, hit a key to logout and exit");
95 try { System.in.read(); } catch(java.io.IOException e) {}
96 con.disconnect(new DisconnectQos(glob));
97 }
98 }
99
100 /**
101 * Try
102 * <pre>
103 * java org.xmlBlaster.test.memoryleak.PublishErase -bulkSize 200
104 * </pre>
105 * for usage help
106 */
107 public static void main(String args[]) {
108 Global glob = new Global();
109
110 if (glob.init(args) != 0) { // Get help with -help
111 System.out.println(glob.usage());
112 System.err.println("Example: java PublishErase -loginName Jeff\n");
113 System.exit(1);
114 }
115
116 new PublishErase(glob);
117 }
118 }
syntax highlighted by Code2HTML, v. 0.9.1