Adding Activities to a Third Party feed with the Java API
Activity Streams 5.0 and later.
Activity Streams 5.0 includes a new, lightweight API that allows plugins to add activities to the Activity Stream. This API doesn't give you as much power as building a full-fledged Activity Provider, but it's extremely easy to use.
We'll demonstrate the API by building a sample plugin that uses a JIRA issue event listener to add an activity whenever an issue is deleted.
Create the plugin project
First we need to set up our development environment and create our plugin project and source directories. Assuming you have the Atlassian Plugin SDK installed, execute the following:
Follow the prompts to complete the initial plugin setup. We've used
com.atlassian.labs as our
streams-jira-delete-issue-plugin as our
Add dependencies to the POM
First, we need to add a dependency on the
streams-api artifacts to the
dependencies section in our
The scope should be
provided, as the library is part of Activity Streams 5, which is already bundled in JIRA. To create an event listener, we also need to add a few other dependencies:
We need to import the
ActivityService component in our
We can then use constructor injection whenever we need the
Import other components
To get our event listener example working, we'll also need to inject a few other components from JIRA. We'll add the following lines to our
We're going to use
applicationProperties to fetch the base URL of our JIRA application. We'll need the
eventPublisher to subscribe to JIRA's issue events.
We're going to create a new component that registers itself as an event listener, and responds to deletion events by posting an activity via the
ActivityService. Let's first declare the component in our
As the xml snippet indicates, we'll also want to create a class called
DeleteIssueListener in the
com.atlassian.labs.streams package. Create that class, and add a constructor that injects the components that we need:
As described in our event listener plugin tutorial, this component needs to tie its registration with the
eventPublisher to our plugin's lifecycle. We can do that by implementing
Create the activity
We'll create an activity whenever a JIRA issue is deleted, and post it using our
ActivityService. The code is as follows:
We create a new
Activity by using the
Activity.Builder helper class.
Either<ValidationErrors, Activity>, which is a disjoint union : either we created a valid
Activity, or we received a set of errors indicating the reasons why the
Activity we tried to construct wasn't well formed. The two
for loops are a clean way of handling the two cases : only one of them actually executes, so we're either going to log the errors, or post the valid activity. Hopefully, the latter is the case, and our nice new activity well appear in the feed!
Activities for Deleted Issues