Responding to application events
Using the Atlassian Event framework and the plugin system, Bitbucket Server makes it easy to respond to events such as projects being created, removed and repositories being pushed or pulled from.
How to respond to events
- Create a Java class called
MyEventListener
. - Create a new method that takes a single event (in our example,
RepositoryPushEvent
) as an argument and annotate it with@EventListener
. This annotation tells the event framework that the method is listening for the specified type of event. The method name does not matter. - Implement the
@EventListener
method to respond to the event however you like. - Ensure that an instance of your class is registered with the
EventPublisher
. Starting from version 4.7, Bitbucket will automatically scan any components annotated with@Named
and register all methods annotated with@EventListener
. Note that if your plugin targets versions before 4.7, you'll need to manually register and unregister your event listener.
Example - automatic registration - suitable for Bitbucket 4.7 and higher
MyEventListener.java
x1package com.atlassian.bitbucket.example;
2
3import com.atlassian.bitbucket.event.repository.RepositoryPushEvent;
4import com.atlassian.event.api.EventListener;
5import org.slf4j.Logger;
6import org.slf4j.LoggerFactory;
7
8import javax.inject.Named;
9
10"myEventListener") (
11public class MyEventListener {
12private static final Logger log = LoggerFactory.getLogger(MyEventListener.class);
13
14//Whenever a push is made, the RepositoryPushEvent is fired and this listener is called
15
16public void onPushEvent(RepositoryPushEvent pushEvent) {
17log.info("A push was made to {}", pushEvent.getRepository());
18}
19}
Example - manual registration - suitable for Bitbucket 4.0 and higher
This method is safe to use in Bitbucket 4.7 and higher as well. In 4.7 and higher, manual registration will result in the component being registered twice (because starting with 4.7 any @Named
components are automatically scanned and registered). This is not a problem because the EventPublisher
will recognize this and ensure that events are sent to your listener only once.
MyEventListener.java
431package com.atlassian.bitbucket.example;
2
3import com.atlassian.bitbucket.event.repository.RepositoryPushEvent;
4import com.atlassian.event.api.EventListener;
5import com.atlassian.event.api.EventPublisher;
6import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
7import org.slf4j.Logger;
8import org.slf4j.LoggerFactory;
9
10import javax.annotation.PostConstruct;
11import javax.annotation.PreDestroy;
12import javax.inject.Inject;
13import javax.inject.Named;
14
15"myEventListener") (
16public class MyEventListener {
17private static final Logger log = LoggerFactory.getLogger(MyEventListener.class);
18
19private final EventPublisher eventPublisher;
20
21
22public MyEventListener( EventPublisher eventPublisher) {
23this.eventPublisher = eventPublisher;
24}
25
26
27public void init() {
28// Called after the instance is created (when the plugin is started)
29eventPublisher.register(this);
30}
31
32
33public void destroy() {
34// Called just before the instance is destroyed (when the plugin is stopped)
35eventPublisher.unregister(this);
36}
37
38//Whenever a push is made, the RepositoryPushEvent is fired and this listener is called
39
40public void onPushEvent(RepositoryPushEvent pushEvent) {
41log.info("A push was made to {}", pushEvent.getRepository());
42}
43}
Asynchronous events
Any event marked with the @AsynchronousPreferred
annotation is likely to be executed on a pool thread, so any code subscribing to these events should be thread safe.
Available events
All events fired by Bitbucket Server core live in com.atlassian.bitbucket.event and its child packages (see for example com.atlassian.bitbucket.event.pull and com.atlassian.bitbucket.event.permission).
Please grab the source or browse the javadoc for a full listing of available event classes.