Skip to content

Instantly share code, notes, and snippets.

@aschweer
Created June 29, 2017 03:59
Show Gist options
  • Save aschweer/f2aba29a463039a2252c9cbdaa318988 to your computer and use it in GitHub Desktop.
Save aschweer/f2aba29a463039a2252c9cbdaa318988 to your computer and use it in GitHub Desktop.
DSpace event consumer that sends JMS message on item install

This is very rough proof of concept code of a DSpace event consumer that sends a message to a JMS topic when an item is made live in DSpace.

Written for DSpace 6.

There are two classes - the actual event consumer and a data model class that holds very basic data about the DSpace item. This is used in the consumer to generate a JSON message corresponding to the model class.

Add the two classes to the additions module, change dspace.cfg as shown in the diff, and add two dependencies to the additions module pom:

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>3.2.16.RELEASE</version>
  </dependency
  
  <dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-client</artifactId>
    <version>5.14.3</version>
  </dependency>

As presented at Open Repositories 2017.

diff --git a/dspace/config/dspace.cfg b/dspace/config/dspace.cfg
index cbb6130..688107b 100644
--- a/dspace/config/dspace.cfg
+++ b/dspace/config/dspace.cfg
@@ -634,7 +634,7 @@ event.dispatcher.default.class = org.dspace.event.BasicDispatcher
# Add doi here if you are using org.dspace.identifier.DOIIdentifierProvider to generate DOIs.
# Adding doi here makes DSpace send metadata updates to your doi registration agency.
# Add rdf here, if you are using dspace-rdf to export your repository content as RDF.
-event.dispatcher.default.consumers = versioning, discovery, eperson
+event.dispatcher.default.consumers = versioning, discovery, eperson, publish
# The noindex dispatcher will not create search or browse indexes (useful for batch item imports)
event.dispatcher.noindex.class = org.dspace.event.BasicDispatcher
@@ -649,6 +649,8 @@ event.consumer.eperson.class = org.dspace.eperson.EPersonConsumer
event.consumer.eperson.filters = EPerson+Create
+event.consumer.publish.class = net.schweerelos.dspace.event.PublishOnInstall
+event.consumer.publish.filters = Item+Install
# consumer to update metadata of DOIs
event.consumer.doi.class = org.dspace.identifier.doi.DOIConsumer
package net.schweerelos.dspace.event;
import org.dspace.content.Item;
import org.dspace.core.Context;
import org.dspace.handle.service.HandleService;
import java.io.Serializable;
import java.sql.SQLException;
/**
* @author Andrea Schweer schweer@waikato.ac.nz
* for the University of Waikato's Institutional Research Repositories
*/
class MessageItem implements Serializable
{
String title;
String authorName;
String authorEmail;
String url;
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getAuthorName()
{
return authorName;
}
public void setAuthorName(String authorName)
{
this.authorName = authorName;
}
public String getAuthorEmail()
{
return authorEmail;
}
public void setAuthorEmail(String authorEmail)
{
this.authorEmail = authorEmail;
}
public String getUrl()
{
return url;
}
public void setUrl(String url)
{
this.url = url;
}
MessageItem(Item item,
Context context,
HandleService handleService)
{
title = item.getName();
authorName = item.getSubmitter().getFullName();
authorEmail = item.getSubmitter().getEmail();
try
{
url = handleService.resolveToURL(context, item.getHandle());
}
catch (SQLException | NullPointerException e)
{
e.printStackTrace();
}
}
}
package net.schweerelos.dspace.event;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.log4j.Logger;
import org.dspace.content.Item;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.event.Consumer;
import org.dspace.event.Event;
import org.dspace.handle.factory.HandleServiceFactory;
import org.dspace.handle.service.HandleService;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
import org.springframework.jms.support.converter.MessageType;
/**
* @author Andrea Schweer schweer@waikato.ac.nz
* for the University of Waikato's Institutional Research Repositories
*/
public class PublishOnInstall implements Consumer
{
private static final Logger log = Logger.getLogger(PublishOnInstall.class);
private JmsTemplate jmsTemplate;
@Override
public void initialize() throws Exception
{
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin",
"admin",
"tcp://localhost:61616");
jmsTemplate = new JmsTemplate(connectionFactory);
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
jmsTemplate.setMessageConverter(converter);
}
@Override
public void consume(Context ctx, Event event) throws Exception
{
if (event == null || event.getEventType() != Event.INSTALL ||
event.getSubjectType() !=
Constants.ITEM || event.getSubject(ctx) == null)
{
return;
}
Item item = (Item) event.getSubject(ctx);
HandleService handleService = HandleServiceFactory.getInstance().getHandleService();
MessageItem msgItem = new MessageItem(item, ctx, handleService);
jmsTemplate.convertAndSend(new ActiveMQTopic("T_IRR_ITEM"), msgItem);
}
@Override
public void end(Context ctx) throws Exception
{
}
@Override
public void finish(Context ctx) throws Exception
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment