<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dariusz [LocK] Łuksza &#187; xmpp-pubsub</title>
	<atom:link href="http://luksza.org/category/programing/java/xmpp-pubsub/feed/" rel="self" type="application/rss+xml" />
	<link>http://luksza.org</link>
	<description>myśli luźno zebrane ... ja i moja jaźń w intenecie</description>
	<lastBuildDate>Fri, 30 Jul 2010 01:22:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>

   <image>
    <title>Dariusz [LocK] Łuksza</title>
    <url>http://0.gravatar.com/avatar/ed2d261ca5db36a17e690dc736dcd9ef?s=48&amp;d=http%3A%2F%2Fluksza.org%2Fwp-includes%2Fimages%2F</url>
    <link>http://luksza.org</link>
   </image>
		<item>
		<title>XMPP-PubSub w Java (cz. 2)</title>
		<link>http://luksza.org/2008/11/10/xmpp-pubsub-w-java-cz-2/</link>
		<comments>http://luksza.org/2008/11/10/xmpp-pubsub-w-java-cz-2/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 11:47:52 +0000</pubDate>
		<dc:creator>Dariusz Łuksza</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[polish]]></category>
		<category><![CDATA[programowanie]]></category>
		<category><![CDATA[xmpp-pubsub]]></category>
		<category><![CDATA[publish]]></category>
		<category><![CDATA[pubsub]]></category>
		<category><![CDATA[subscribe]]></category>
		<category><![CDATA[xmpp]]></category>

		<guid isPermaLink="false">http://luksza.org/?p=173</guid>
		<description><![CDATA[W poprzednim wpisie, pokazałem jak wystawić pojedynczy węzeł (node), dziś zrobię/zrobimy trochę więcej, a mianowicie opublikujemy &#8222;wiadomość&#8221;, a przede wszystkim odbierzemy zdarzenie o utworzeniu wiadomości. Przecież o tą zaletę XMPP-PubSub nam (a przynajmniej mi ) najbardziej chodzi &#8230; o jego zdarzeniowy charakter ;&#62; &#8230; a więc do roboby Żeby to  co piszę było bardziej uniwersalne, celem [...]]]></description>
			<content:encoded><![CDATA[<p>W <a href="http://luksza.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2x1a3N6YS5vcmcvMjAwOC8xMS8wMS94bXBwLXB1YnN1Yi13LWphdmEtaS1uaWUtdHlsa28tY3oxLw==">poprzednim wpisie</a>, pokazałem jak wystawić pojedynczy węzeł (node), dziś zrobię/zrobimy trochę więcej, a mianowicie opublikujemy &#8222;wiadomość&#8221;, a przede wszystkim odbierzemy zdarzenie o utworzeniu wiadomości. Przecież o tą zaletę XMPP-PubSub nam (a przynajmniej mi <img src='http://luksza.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ) najbardziej chodzi &#8230; o jego zdarzeniowy charakter ;&gt; &#8230; a więc do roboby <img src='http://luksza.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><span id="more-173"></span></p>
<p>Żeby to  co piszę było bardziej uniwersalne, celem wprowadzenia warte opisania są właściwości zdarzeniowe obu bibliotek do obsługi XMPP (mam tu na myśli smack (do obsługi samego XMPP) i su-smack (do obsługi XMPP-PubSub)). Pewnie bardziej dociekliwy czytelnik zauważył w <a href="http://luksza.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2x1a3N6YS5vcmcvMjAwOC8xMS8wMS94bXBwLXB1YnN1Yi13LWphdmEtaS1uaWUtdHlsa28tY3oxLw==">poprzedniej części</a> dodanie <code>PacketListener</code>&#8216;a do obiektu połączenia, dla mniej dociekliwych i uważnych poniżej znajduje się ten kawałek kodu:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">con.<span style="color: #006633;">addPacketListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PacketListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> processPacket<span style="color: #009900;">&#40;</span>Packet pkt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>pkt.<span style="color: #006633;">toXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>, <span style="color: #006600; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Co ja tutaj takiego zrobiłem ? &#8230; W ten sposób rozwiałem problem z debug&#8217;owaniem pakietów jakie wracały z serwera, gwoli ścisłości metoda <code>addPacketListener</code> przyjmuje 2 parametry, pierwszy z nich jest to wspominany w nazwie <code>PacketListener</code> (interfejs z jedną metodą void <code>processPacket(Packet pkt)</code>), oraz obiekt <code>PacketFilter</code>. Dzięki takiemu rozwiązaniu programista jest odciążony z obowiązku własnoręcznego oprogramowywania otrzymywanych pakietów. Mały przykład, załóżmy że interesują nas tylko wiadomości od jednego użytkownika &#8230; nic prostszego:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">PacketFilter filter = <span style="color: #000000; font-weight: bold;">new</span> AndFilter<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PacketTypeFilter<span style="color: #009900;">&#40;</span>Message.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>,
            <span style="color: #000000; font-weight: bold;">new</span> FromContainsFilter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my-love@jabber.swh&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
conn.<span style="color: #006633;">addPacketFilter</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PacketFilter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> processPacket<span style="color: #009900;">&#40;</span>Packet pkt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;My love is writing to me ;).&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>, filter <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>W ten oto prosty sposób mamy rozwiązany powyższy problem <img src='http://luksza.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>Jeżeli chodzi natomiast nasłuchiwanie na pakiety PubSub  sprawa jest odrobinę bardziej zakręcona &#8230; biblioteka su-smack posiada jedna pomocniczą klasę <code>XMPPUtils</code> która jest bardzo ciekawa ;&gt; posiada ona tylko 5 metod (z czego 2 są dodatkowo przeciążone), na chwilę obecną będą nas interesować tylko 2 obecne tam publiczne statyczne metody: <code>createEventPacketListener(PubSubEventListener listener)</code> oraz <code>createPubSubFilter()</code>. Pierwsza z nich zwraca obiekt <code>PacketListener<code> dla przekaznego w parametrze obiektu <code>PubSubEventListener</code>, a co robi druga jest bardzo łatwo się domyślić <img src='http://luksza.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>Dodatkowo nasz program kliencki będzie się subskrybował do węzła ... kod subskrypcji jest bardzo prosty i nie wymaga opisu IMHO. Poniżej znajduje się kompletny kod klienta:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
</pre></td><td class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
 *     Published on GNU GPLv2
 */</span>
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.luksza.xmpp.pubsub</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jivesoftware.smack.ConnectionConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jivesoftware.smack.PacketCollector</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jivesoftware.smack.PacketListener</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jivesoftware.smack.XMPPConnection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jivesoftware.smack.filter.PacketIDFilter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jivesoftware.smack.packet.Packet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jivesoftware.smack.packet.IQ.Type</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">se.su.it.smack.pubsub.PubSub</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">se.su.it.smack.pubsub.PubSubEventListener</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">se.su.it.smack.pubsub.elements.DeleteElement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">se.su.it.smack.pubsub.elements.ItemElement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">se.su.it.smack.pubsub.elements.PurgeElement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">se.su.it.smack.pubsub.elements.RetractElement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">se.su.it.smack.pubsub.elements.SubscribeElement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">se.su.it.smack.utils.XMPPUtils</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author Dariusz [LocK] Łuksza
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SubscribeToNode <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399; font-weight: bold;">String</span> login = <span style="color: #0000ff;">&quot;pubsub-test&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399; font-weight: bold;">String</span> pass = <span style="color: #0000ff;">&quot;pubsub-test&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399; font-weight: bold;">String</span> pubSubServerUrl = <span style="color: #0000ff;">&quot;pubsub.server&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #006600; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> XMPPConnection con = <span style="color: #000000; font-weight: bold;">new</span> XMPPConnection<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ConnectionConfiguration<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;server&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Connecting ...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            con.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000;  font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>con.<span style="color: #006633;">isConnected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; DONE.<span style="color: #000099; font-weight: bold;">\n</span>Logging ...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                con.<span style="color: #006633;">login</span><span style="color: #009900;">&#40;</span>login, pass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000;  font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>con.<span style="color: #006633;">isAuthenticated</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; DONE.<span style="color: #000099; font-weight: bold;">\n</span>Subscribing to node ...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000000; font-weight: bold;">final</span> PubSub pubSubSubscribe = <span style="color: #000000; font-weight: bold;">new</span> PubSub<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    pubSubSubscribe.<span style="color: #006633;">setTo</span><span style="color: #009900;">&#40;</span>pubSubServerUrl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    pubSubSubscribe.<span style="color: #006633;">setType</span><span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Type</span>.<span style="color: #006633;">SET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    pubSubSubscribe.<span style="color: #006633;">setFrom</span><span style="color: #009900;">&#40;</span>con.<span style="color: #006633;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000000; font-weight: bold;">final</span> SubscribeElement subscribe = <span style="color: #000000; font-weight: bold;">new</span> SubscribeElement<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/home/server/blee&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    subscribe.<span style="color: #006633;">setJid</span><span style="color: #009900;">&#40;</span>con.<span style="color: #006633;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    pubSubSubscribe.<span style="color: #006633;">addChild</span><span style="color: #009900;">&#40;</span>subscribe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    XMPPUtils.<span style="color: #006633;">sendAndWait</span><span style="color: #009900;">&#40;</span>con, pubSubSubscribe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// subskrybcja jest zawsze akceptowana.</span>
                    <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; DONE.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                    PacketListener pubsubPacketListner = XMPPUtils.<span style="color: #006633;">createEventPacketListener</span><span style="color: #009900;">&#40;</span>
                                <span style="color: #000000; font-weight: bold;">new</span> MyPubSusEventListener<span style="color: #009900;">&#40;</span>con<span style="color: #009900;">&#41;</span>
                            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    con.<span style="color: #006633;">addPacketListener</span><span style="color: #009900;">&#40;</span>pubsubPacketListner, XMPPUtils.<span style="color: #006633;">createPubSubFilter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                    XMPPUtils.<span style="color: #006633;">sendAndWait</span><span style="color: #009900;">&#40;</span>con, pubSubSubscribe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #000000;  font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #003399; font-weight: bold;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
            con.<span style="color: #006633;">disconnect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> MyPubSusEventListener <span style="color: #000000; font-weight: bold;">implements</span> PubSubEventListener <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> XMPPConnection _con<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> MyPubSusEventListener<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> XMPPConnection con<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            _con = con<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> onDelete<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> DeleteElement element<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">Exception</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Deleted element: &quot;</span> + element.<span style="color: #006633;">toXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// w parametrze przekazywana jest tylko nazwa utworzonego elemnu</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> onPublish<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> ItemElement elemnt<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">Exception</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>Published element:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> + elemnt.<span style="color: #006633;">toXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// zeby odebrac dane trzeba opakowac otrzymany element</span>
            <span style="color: #666666; font-style: italic;">// w &quot;caly pakiet&quot;</span>
            <span style="color: #000000; font-weight: bold;">final</span> PubSub getItem = <span style="color: #000000; font-weight: bold;">new</span> PubSub<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            getItem.<span style="color: #006633;">setType</span><span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Type</span>.<span style="color: #006633;">GET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            getItem.<span style="color: #006633;">setTo</span><span style="color: #009900;">&#40;</span>pubSubServerUrl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            getItem.<span style="color: #006633;">setFrom</span><span style="color: #009900;">&#40;</span>_con.<span style="color: #006633;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            getItem.<span style="color: #006633;">addChild</span><span style="color: #009900;">&#40;</span>elemnt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
&nbsp;
            <span style="color: #666666; font-style: italic;">// zeby odebrac dane trzeba poczekac na pakiet z id</span>
            <span style="color: #666666; font-style: italic;">// takim samym jak wyslane</span>
            <span style="color: #000000; font-weight: bold;">final</span> PacketCollector collector = _con.<span style="color: #006633;">createPacketCollector</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #000000; font-weight: bold;">new</span> PacketIDFilter<span style="color: #009900;">&#40;</span>getItem.<span style="color: #006633;">getPacketID</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            _con.<span style="color: #006633;">sendPacket</span><span style="color: #009900;">&#40;</span>getItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// wysylanie</span>
            <span style="color: #000000; font-weight: bold;">final</span> Packet result = collector.<span style="color: #006633;">nextResult</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// oczekiwanie na wynik</span>
&nbsp;
            <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>Retrive data:&quot;</span> + result.<span style="color: #006633;">toXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> onPurge<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> PurgeElement elemnt<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">Exception</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Purged element: &quot;</span> + elemnt.<span style="color: #006633;">toXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> onRetract<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> RetractElement elemnt<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">Exception</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Retracted element: &quot;</span> + elemnt.<span style="color: #006633;">toXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Kod "serwera" (tj. aplikacji publikującej dane) jest bardzo prosty, i sprowadza się do użycia odpowiednich klas i ich wysłania ...</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
 * 	Published on GNU GPLv2
 */</span>
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.luksza.xmpp.pubsub</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jivesoftware.smack.PacketListener</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jivesoftware.smack.XMPPConnection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jivesoftware.smack.packet.Packet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jivesoftware.smack.packet.IQ.Type</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">se.su.it.smack.pubsub.PubSub</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">se.su.it.smack.pubsub.elements.ItemElement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">se.su.it.smack.pubsub.elements.PublishElement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">se.su.it.smack.utils.XMPPUtils</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author Dariusz [LocK] Łuksza
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PubSubCreateNode <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #006600; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399; font-weight: bold;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> argv<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">Exception</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399; font-weight: bold;">String</span> login = <span style="color: #0000ff;">&quot;blee&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399; font-weight: bold;">String</span> serverHost = <span style="color: #0000ff;">&quot;server&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> XMPPConnection con = <span style="color: #000000; font-weight: bold;">new</span> XMPPConnection<span style="color: #009900;">&#40;</span>serverHost<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        con.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            con.<span style="color: #006633;">login</span><span style="color: #009900;">&#40;</span>login, <span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000;  font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>con.<span style="color: #006633;">isAuthenticated</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">final</span> PubSub pubSub = <span style="color: #000000; font-weight: bold;">new</span> PubSub<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                pubSub.<span style="color: #006633;">setType</span><span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Type</span>.<span style="color: #006633;">SET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                pubSub.<span style="color: #006633;">setFrom</span><span style="color: #009900;">&#40;</span>con.<span style="color: #006633;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                pubSub.<span style="color: #006633;">setTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pubsub.&quot;</span>  + serverHost<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000000; font-weight: bold;">final</span> PublishElement publish = <span style="color: #000000; font-weight: bold;">new</span> PublishElement<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/home/&quot;</span> + serverHost + <span style="color: #0000ff;">&quot;/&quot;</span> + login<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">final</span> ItemElement item = <span style="color: #000000; font-weight: bold;">new</span> ItemElement<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Test Item&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                item.<span style="color: #006633;">setContent</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;entry&gt;Test TEST test&lt;/entry&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                publish.<span style="color: #006633;">addChild</span><span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                pubSub.<span style="color: #006633;">addChild</span><span style="color: #009900;">&#40;</span>publish<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                XMPPUtils.<span style="color: #006633;">sendAndWait</span><span style="color: #009900;">&#40;</span>con, pubSub<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000;  font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Not authenticated!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
            con.<span style="color: #006633;">disconnect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Jak to działa ? Najpierw odpalamy "klienta" który subskrybuje się do węzła i nasłuchuje (w nie skończoność) na nowe dane. Potem odpalamy kod publikujący dane, aplikacja się spokojnie wykona, a w kliencie dostaniemy informacje o tym, że została opublikowana nowa wiadomość jak i od razu ją odbieramy <img src='http://luksza.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Ciekawe linki:</p>
<ul>
<li>oczywiście <a href="http://luksza.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3htcHAub3JnL2V4dGVuc2lvbnMveGVwLTAwNjAuaHRtbCNzdWJzY3JpYmVyLXJldHJpZXZl" target=\"_blank\">XEP-0060</a></li>
<li><a href="http://luksza.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3RvZGF5LmphdmEubmV0L3B1Yi9hL3RvZGF5LzIwMDYvMTAvMDUvaW5zdGFudC1tZXNzYWdpbmctZm9yLWphYmJlci13aXRoLXNtYWNrLmh0bWw=" target=\"_blank\">ciekawy artykuł</a>, nie przeczytałem całego <img src='http://luksza.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ale zamieszczone tam kody  pomogły mi zrozumieć parę rzeczy <img src='http://luksza.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </li>
</ul>
 <img src="http://luksza.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=173" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://luksza.org/2008/11/10/xmpp-pubsub-w-java-cz-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>XMPP-PubSub w Java i nie tylko (cz. 1)</title>
		<link>http://luksza.org/2008/11/01/xmpp-pubsub-w-java-i-nie-tylko-cz1/</link>
		<comments>http://luksza.org/2008/11/01/xmpp-pubsub-w-java-i-nie-tylko-cz1/#comments</comments>
		<pubDate>Sat, 01 Nov 2008 17:07:48 +0000</pubDate>
		<dc:creator>Dariusz Łuksza</dc:creator>
				<category><![CDATA[fun]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[polish]]></category>
		<category><![CDATA[programowanie]]></category>
		<category><![CDATA[xmpp-pubsub]]></category>
		<category><![CDATA[pubsub]]></category>
		<category><![CDATA[smack]]></category>
		<category><![CDATA[su-smack]]></category>
		<category><![CDATA[xmpp]]></category>

		<guid isPermaLink="false">http://luksza.org/?p=148</guid>
		<description><![CDATA[W ramach eksternistycznego zaliczenia przedmiotu Systemy internetowe, muszę wykonać aplikację w architekturze SOA z wykorzystaniem XMPP-PubSub (XEP-0060). Jako, że temat jest ciekawy i (IMHO) przyszłościowy postaram się go przybliżyć w paru notkach. Czego można się po tych notkach spodziewać ? Przede wszystkim działających (w chwili publikacji ) fragmentów kodu, opisów problemów jakie napotkałem podczas realizacji [...]]]></description>
			<content:encoded><![CDATA[<p>W ramach eksternistycznego zaliczenia przedmiotu Systemy internetowe, muszę wykonać aplikację w architekturze SOA z wykorzystaniem</p>
<p><a href="http://luksza.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3htcHAub3JnL2V4dGVuc2lvbnMveGVwLTAwNjAuaHRtbA==" target=\"_blank\">XMPP-PubSub (XEP-0060)</a>. Jako, że temat jest ciekawy i (IMHO) przyszłościowy postaram się go przybliżyć w paru notkach.</p>
<p>Czego można się po tych notkach spodziewać ? Przede wszystkim działających (w chwili publikacji <img src='http://luksza.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ) fragmentów kodu, opisów problemów jakie napotkałem podczas realizacji projektu &#8230; oraz ich rozwiązań. Wszystko oczywiście, jak to u mnie bywa, w mniej lub bardziej chaotycznej formie ;&gt;</p>
<p><span id="more-148"></span></p>
<p>Na początek może trochę teorii i historii (oczywiście z mojego punktu widzenia).</p>
<p>Co to jest XMPP ? Cóż, część osób będzie kojarzyć ten termin z Jabber&#8217;em &#8230; i słusznie, sam Jabber jest przodkiem XMPP , jako że całość bazuje na XML&#8217;u jest to <em>potężne</em> &#8222;<em>narzędzie</em>&#8221; mogące służyć nie tylko do komunikacji w internecie, ale też do wielu innych celów.</p>
<p>Co to jest PubSub ? Właściwie to Publish-Subscribe; jest to jedna z wielu &#8221; usług&#8221; jakie udostępnia XMPP, umożliwia ona &#8222;wystawianie&#8221; wiadomości. Dzięki wykorzystaniu Observer patter eliminuje zbędny ruch sieciowy (klient nie pyta serwera co 5 min &#8222;masz już coś dla mnie?&#8221; &#8230; &#8222;a może teraz masz ?&#8221;), umożliwa praktycznie natychmiastowe informowanie klientów o zmianach/nowościach.</p>
<p>Z czym to się je ? Właściwie to ze wszystkim i niczym &#8230; tj. na pewno potrzebny jest serwer Jabbera z zaimplementowaną obsługa pub-sub (może to być np. ejabberd2 czy jabberd2, ja będę używał tego pierwszego z racji że &#8222;posiadam&#8221; go od paru lat <img src='http://luksza.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ) i to by był na tyle &#8230; no ale w sumie to jeszcze można by z tego zrobić jakis użytek, dosłownie &#8222;jakiś&#8221;. Tak więc będą nam do tego potrzebne jakieś biblioteki &#8230; z takowymi nie ma problemu, chwila googlania zaprowadziła mnie do (wspominanego wyżej) smack&#8217;a i su-smack&#8217;a. Ok, całkiem fajnie, mi się to podoba &#8230; tylko po co mi do tego jeszcze jakiś dodatkowy serwer i to jeszcze jabbera ? Jest on po to, żeby ułatwić sobie życie, robi on za taki &#8222;programowy switch&#8221;, z wykorzystaniem smack&#8217;a można na takim serwerze wystawić node&#8217;a z danymi i odpowiednią konfiguracją &#8230; i o nim poprostu zapomnieć, dalej nasza zewnętrzna aplikacja (czyli OpenSource&#8217;owy, całkowicie darmowy soft) zrobi za nas wszystko, tj. roześle informacje do tych klientów którzy się zapisali na listę chętnych do odebrania informacji (tutaj jest właśnie nasz Observer pattern). Architektura jest bardzo prosta, klient podłącza się do serwera i zapisuje się (lub jak kto woli subscribe&#8217;uje), na zdarzenia (eventy), z drugiej strony łączy się inny klient czy serwer i wystawia dane (tu się znowu powtórzę) reszte robi za nas Observer pattern i serwer jabbera. &#8230; Ok wszystko fajnie, no ale ja nie chce czy też nie mogę/nie umiem/nie mam gdzie (nie potrzebne skreślić) postawić taki serwer. Nie musisz ;P możesz użyć jakiegoś publicznego, wystarczy rejestracja (a czasem nawet i to nie jest wymagane).</p>
<p><a href="http://luksza.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5pZ25pdGVyZWFsdGltZS5vcmcvcHJvamVjdHMvc21hY2sv" target=\"_blank\">Smack</a> i <a href="http://luksza.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3N0YXRpYy5kZXZlbC5pdC5zdS5zZS9zdS1zbWFjay8=" target=\"_blank\">su-smack</a>. Obie biblioteki są open-source&#8217;owe, pierwszy na licencji Apache, a drugi na Stockholm university BSD License. Więc nie będą sprawiać żadnych problemów ;&gt;. Obie są w całości napisane w Java&#8217;ie, trafiłem na nie po chwili google&#8217;ania <img src='http://luksza.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>OK, koniec tego długiego i nudnego wstępu, czas zabrać się za to co tygryski lubią najbardziej ;&gt; &#8230; czyli konfiguracje &#8230; żartuje, oczywiście najlepszą częścią będzie kodowanie, ale chcąc być trendy i wogole super &#8230; zwłaszcza, że oba projekty na to pozwalają &#8230; chciał bym przedstawić konfigurację <a href="http://luksza.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL21hdmVuLmFwYWNoZS5vcmcv" target=\"_blank\">maven</a>&#8216;a. W pliku pom.xml w sekcji repositories dodajemy repo (lub jak kto woli można to zrobić w $M2_HOME/setttings.xml, ale lepiej nie uzależniać projektu od globalnej konfiguracji <img src='http://luksza.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> )</p>
<p><code>&lt;repository&gt;<br />
&lt;id&gt;it.su.se&lt;/id&gt;<br />
&lt;name&gt;su-smack&lt;/name&gt;<br />
&lt;url&gt;http://maven.it.su.se/it.su.se/maven2&lt;/url&gt;<br />
&lt;/repository&gt;</code></p>
<p>natomiast do sekcji dependenties dodajemy:</p>
<p><code>&lt;dependency&gt;<br />
&lt;artifactId&gt;su-smack&lt;/artifactId&gt;<br />
&lt;groupId&gt;su-smack&lt;/groupId&gt;<br />
&lt;version&gt;1.4-SNAPSHOT&lt;/version&gt;<br />
&lt;/dependency&gt;</code></p>
<p>Teraz właściwa część notki &#8230; kod ;&gt;. Najpierw założenia, chcemy się podlaczyć do serwera jabber&#8217;a, zalogować się i stworzyć w węzeł, dodatkowo dodam PacketListener żeby widzieć jak wygladą komunikacja pomiędzy serwerem jabber&#8217;a, a kodem. Kod nie jest zaawansowany więc nie sądze żeby wymagał opisu.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
 * 	Published on GNU GPLv2
 */</span>
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.luksza.xmpp.pubsub</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jivesoftware.smack.PacketListener</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jivesoftware.smack.XMPPConnection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jivesoftware.smack.packet.Packet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jivesoftware.smack.packet.IQ.Type</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">se.su.it.smack.pubsub.PubSub</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">se.su.it.smack.pubsub.elements.ConfigureElement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">se.su.it.smack.pubsub.elements.CreateElement</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
* @author Dariusz [LocK] Łuksza
*/</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PubSubCreateNode <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #006600; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399; font-weight: bold;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> argv<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">Exception</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399; font-weight: bold;">String</span> login = <span style="color: #0000ff;">&quot;login&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> XMPPConnection con = <span style="color: #000000; font-weight: bold;">new</span> XMPPConnection<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jabber.host&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        con.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        con.<span style="color: #006633;">addPacketListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PacketListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> processPacket<span style="color: #009900;">&#40;</span>Packet pkt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>pkt.<span style="color: #006633;">toXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>, <span style="color: #006600; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            con.<span style="color: #006633;">login</span><span style="color: #009900;">&#40;</span>login, <span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000;  font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>con.<span style="color: #006633;">isAuthenticated</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">final</span> PubSub pubSub = <span style="color: #000000; font-weight: bold;">new</span> PubSub<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                pubSub.<span style="color: #006633;">setType</span><span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Type</span>.<span style="color: #006633;">SET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                pubSub.<span style="color: #006633;">setTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pubsub.jabber.host&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                pubSub.<span style="color: #006633;">setFrom</span><span style="color: #009900;">&#40;</span>con.<span style="color: #006633;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                pubSub.<span style="color: #006633;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> CreateElement<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/home/jabber.host/&quot;</span> + login<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                pubSub.<span style="color: #006633;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ConfigureElement<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                con.<span style="color: #006633;">sendPacket</span><span style="color: #009900;">&#40;</span>pubSub<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #003399; font-weight: bold;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000;  font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003399; font-weight: bold;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Not authenticated!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
            con.<span style="color: #006633;">disconnect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Po uruchowmieniu, używając jakiegoś klienta jabber&#8217;a (tkabber&#8217;a czy np. psi) możemy sprawdzić czy węzeł został utworzony. Należy pamiętać o tym że nie można tworzyć wiely węzłów naraz.I to by było chyba na tyle <img src='http://luksza.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
 <img src="http://luksza.org/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=148" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://luksza.org/2008/11/01/xmpp-pubsub-w-java-i-nie-tylko-cz1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
