One of the aspects of DSpace API that I like most is the Event System, please note that here I am not talking about the DSpace REST API, I am talking about DSpace API, the kernel of DSpace. You should suppose by the title of this post that I will not do a very complex DSpace consumer, but a very simple one, so the aim of this post is talking about how to do a consumer itself, once you get it, you can write your own consumer to do all crazy stuff that you need to do.

First let me explain little bit about the Event System in DSpace. When you interact with DSpace, searching, create/edit/delete an item, or a collection, or a community and even when you are harvesting a collection in DSpace your action create a event by a producer and then the event run a chain of consumer, and those consumers will be process the event. So the consumer that I am about to show you will be trigger by a set of events and do something.

I will create a consumer that will get a event and write the type of the event in DSpace log, I said that it was useless. I want the post to be pretty straightforward, then for now on I will describe the step to do the consumer. For this example I am using DSpace 5.5.

1- I created a class “UselessEventConsumer.java” in the package “com.github.luizclaudiosantos.impl”. You can see my project structure in the picture below, is important to put your code in the folder additions inside of modules, it will save you a lot of headache when you update your DSpace.

dspace-consumer-in-project

2- Let talk about the code of the consumer itself, you can see it in the picture below. The class is not a big deal, in the line 11, I am implementing the intreface org.dspace.event.Consumer, in the line 23, the method consume will be called when the event get triggered by the producer, and then, write the type of the event in the DSpace log.


package com.github.luizclaudiosantos.impl;

import org.apache.log4j.Logger;
import org.dspace.core.Context;
import org.dspace.event.Consumer;
import org.dspace.event.Event;

/**
 * Created by Luiz Claudio Santos on 10/27/16.
 */
public class UselessEventConsumer implements Consumer{

    /** log4j logger */
    private static Logger log = Logger.getLogger(UselessEventConsumer.class);

    @Override
    public void initialize() throws Exception {

    }

    @Override
    public void consume(Context ctx, Event event) throws Exception {
        log.info("The UselessEventConsumer get the event: " + event.getEventTypeAsString());
    }

    @Override
    public void end(Context ctx) throws Exception {

    }

    @Override
    public void finish(Context ctx) throws Exception {

    }
}




3- I need to say to DSpace that it should use my consumer, to do it I added the line below in the file dspace.cfg. In the line I am saying to DSpace that my class “com.github.luizclaudiosantos.impl.UselessEventConsumer.java” is a consumer and it is called useless.

event.consumer.useless.class = com.github.luizclaudiosantos.impl.UselessEventConsumer


4- In the line below I am saying to DSpace that my consumer called useless should be used when the item get modifed, or deleted, or Modify_Metadata or installed. More details about this notation on the DSpace documentations.

event.consumer.useless.filters = Item+Modify|Delete|Modify_Metadata|Install


5- The last thing to do is register the useless consumer as default consumer in the dspace.cfg

event.dispatcher.default.consumers = versioning, discovery, eperson, harvester, useless


Now when I edit the item in DSpace interface like in the picture below I got the line in the log “2016-10-30 16:26:21,206 INFO com.github.luizclaudiosantos.impl.UselessEventConsumer @ The UselessEventConsumer get the event: MODIFY”, very useless!

item_saved

That is it, a very simple and useless consumer, that you can modify to do whatever you want to do, in my opinion this event sytem is one of the most smart way to modify the DSpace default behavior.

Leave a Reply

Your email address will not be published. Required fields are marked *