Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}}
2
3 {{error}}
4 You should check the [[New Notification Tutorial>>WritingEventListenerTutorial]].
5 {{/error}}
6
7 This tutorial uses the old notification mechanism to listen to events (now deprecated in XWiki 2.0). You should follow it if you're using a version of XWiki prior to 2.0.
8
9 In order to listen to events you need to write 2 pages:
10
11 * A page containing a Groovy class that registers against the XWiki Event Manager and that has the method to be called when the event happens.
12 * Another page that parses the Groovy page and loads it in the Groovy context.
13
14 = Groovy Notification Class =
15
16 Your Groovy needs to extend the ##com.xpn.xwiki.notify.XWikiDocChangeNotificationInterface## as shown below.
17
18 {{code language="java"}}
19 /* Groovy Class #* */
20
21 import com.xpn.xwiki.api.*;
22 import com.xpn.xwiki.notify.*;
23 import com.xpn.xwiki.*;
24 import com.xpn.xwiki.doc.*;
25
26 public class MyGroovyClass implements XWikiDocChangeNotificationInterface
27 {
28 def xwiki;
29 def rule;
30
31 public MyGroovyClass()
32 {
33 this.rule = new DocChangeRule(this);
34 }
35
36 public void init(xwiki)
37 {
38 this.xwiki = xwiki;
39 xwiki.getXWiki().getNotificationManager().addGeneralRule(this.rule);
40 }
41
42 public void cleanup()
43 {
44 xwiki.getXWiki().getNotificationManager().removeGeneralRule(this.rule);
45 }
46
47 public void notify(XWikiNotificationRule rule, XWikiDocument newdoc, XWikiDocument olddoc,
48 int event, XWikiContext context)
49 {
50 // Do some action here.
51 }
52 }
53
54 /* *# */
55 {{/code}}
56
57 In this example we've used a ##DocChangeRule## rule. There are also {{scm path="xwiki-platform-core/xwiki-platform-legacy/xwiki-platform-legacy-oldcore/src/main/java/com/xpn/xwiki/notify"}}other rules{{/scm}}.
58
59 = Calling the Groovy Class =
60
61 {{code}}
62 #set($mygroovyclass = $xwiki.parseGroovyFromPage("MySpace.MyGroovyClass"))
63 $mygroovyclass.init($xwiki)
64 {{/code}}
65
66 = Example: IRC notification on document change =
67
68 {{warning}}
69 The code below uses the parseGroovyFromPage method which takes 2 parameters. The second one is the name of page containing JARS as attachments. These JARs are put in the classloader used by Groovy when parsing the page. This feature is only working in XWiki Core 1.3 and later.
70 {{/warning}}
71
72 * **Step 1**: Groovy Class(((
73 {{code language="java"}}
74 /* Groovy Class #* */
75
76 import org.jibble.pircbot.*;
77 import java.util.*;
78 import com.xpn.xwiki.api.*;
79 import com.xpn.xwiki.notify.*;
80 import com.xpn.xwiki.*;
81 import com.xpn.xwiki.doc.*;
82
83 public class XWikiBot extends PircBot implements XWikiDocChangeNotificationInterface
84 {
85 def xwiki;
86 def channel;
87 def rule;
88
89 public XWikiBot()
90 {
91 this.setName("xwikibot");
92 this.rule = new DocChangeRule(this);
93 }
94
95 public void init(xwiki, channel)
96 {
97 this.xwiki = xwiki;
98 this.channel = channel;
99 xwiki.getXWiki().getNotificationManager().addGeneralRule(this.rule);
100 }
101
102 public void cleanup()
103 {
104 xwiki.getXWiki().getNotificationManager().removeGeneralRule(this.rule);
105 }
106
107 public void notify(XWikiNotificationRule rule, XWikiDocument newdoc, XWikiDocument olddoc,
108 int event, XWikiContext context)
109 {
110 sendMessage(this.channel, newdoc.getFullName() + " was modified - " + newdoc.getExternalURL("view", context));
111 }
112 }
113
114 /* *# */
115 {{/code}}
116 )))
117 * **Step 2**: Add the PircBot JAR as an attachment to the ##MySpace.MyGroovyClass## page created in step 1.
118 * **Step 3**: Calling page(((
119 {{code}}
120 ## Start by looking for a bot in the servlet context
121 #set ($sc = $context.getContext().getEngineContext().getServletContext())
122
123 ## If the bot isn't in the servlet context, start the bot and put in the context
124 #set ($bot = $sc.getAttribute("ircbot"))
125 #if (!$bot)
126 Bot is not started, starting it...
127 #set($bot = $xwiki.parseGroovyFromPage("MySpace.MyGroovyClass", "MySpace.MyGroovyClass"))
128 #set ($channel = "#xwiki")
129 $bot.init($xwiki, $channel)
130 $bot.connect("irc.freenode.net")
131 $bot.joinChannel($channel)
132 $sc.setAttribute("ircbot", $bot)
133 Bot started!
134
135 ## If the parameter passed is stop then stop the bot
136 #elseif ($request.action && $request.action == "stop")
137 $bot.cleanup()
138 $bot.disconnect()
139 $sc.setAttribute("ircbot", null)
140 Bot disconnected!
141
142 #else
143 Bot already started, doing nothing...
144
145 #end
146 {{/code}}
147 )))
148 * **Step 4**: Creating a Scheduler job so that the Bot is restarted automatically if the server is restarted for example.(((
149 Create a Scheduler job, set it to run every 5 minutes for example and use the following Groovy script in the job:
150
151 {{code language="java"}}
152 // Start by looking for a bot in the servlet context
153 def sc = context.getEngineContext().getServletContext()
154
155 // If the bot isn't in the servlet context, start the bot and put in the context
156 def bot = sc.getAttribute("ircbot")
157 if (bot == null) {
158 // Bot is not started, starting it...
159 bot = xwiki.parseGroovyFromPage("MySpace.MyGroovyClass", "MySpace.MyGroovyClass")
160 def channel = "#xwiki"
161 bot.init(xwiki, channel)
162 bot.connect("irc.freenode.net")
163 bot.joinChannel(channel)
164 sc.setAttribute("ircbot", bot)
165 // Bot started!
166 }
167 {{/code}}
168 )))

Get Connected