/
java_client_publish.xml
62 lines (58 loc) · 2.02 KB
/
java_client_publish.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?xml version="1.0" encoding="UTF-8"?>
<section version="5.0"
xmlns="http://docbook.org/ns/docbook"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://docbook.org/ns/docbook http://www.docbook.org/xml/5.0/xsd/docbook.xsd"
xml:id="java_client_publish">
<info>
<title>Publishing</title>
</info>
<para>
The following is a typical example of publishing a message on a channel:
</para>
<informalexample>
<programlisting language="java"><![CDATA[
ClientTransport transport = ...
ClientSession client = new BayeuxClient("http://localhost:8080/cometd", transport);
client.handshake();
Map<String, Object> data = new HashMap<String, Object>();
// Fill in the data
client.getChannel("/game/table/1").publish(data);
]]></programlisting>
</informalexample>
<para>
Publishing data on a channel is an asynchronous operation.
</para>
<note>
<para>
Calling <code>publish()</code> <emphasis role="bold">does not</emphasis> mean that you have published the message when <code>publish()</code> returns.
</para>
</note>
<para>
Message batching is also available:
</para>
<informalexample>
<programlisting language="java"><![CDATA[
final ClientSession client = ...;
client.handshake();
client.batch (new Runnable()
{
public void run()
{
Map<String, Object> data1 = new HashMap<String, Object>();
// Fill in the data1 map object
client.getChannel("/game/table/1").publish(data1);
Map<String, Object> data2 = new HashMap<String, Object>();
// Fill in the data2 map object<
client.getChannel("/game/chat/1").publish(data2);
}
});
]]></programlisting>
</informalexample>
<warning>
<para>
The <code>ClientSession</code> API also allows you to batch using <code>startBatch()</code> and <code>endBatch()</code>, but remember to call <code>endBatch()</code> after having called <code>startBatch()</code>, for example in a final block.
If you don't, your messages continue to queue up, and your application does not work as expected.
</para>
</warning>
</section>