Creating an extension project for both event handling and Java ReST API - Creating an extension project for both event handling and Java ReST API - Alfresco - Alfresco Events SDK for Out-of-Process Events - Alfresco/Alfresco-Events-SDK-for-Out-of-Process-Events/6.3/Alfresco-Events-SDK-for-Out-of-Process-Events/Install/Creating-an-extension-project-for-both-event-handling-and-Java-ReST-API - 6.3 - 6.3

Alfresco Events SDK for Out-of-Process Events

Platform
Alfresco
Product
Alfresco Events SDK for Out-of-Process Events
Release
6.3
License
ft:lastPublication
2025-09-04T22:27:16.404000
ft:locale
en-US

Make sure you have completed prerequisites (see Prerequisites) and created a starter project (see Create a starting point Spring project) with configuration properties set for both event handling and ReST API.

The application.properties file should look something like this:

# Where is Alfresco Active MQ JMS Broker running?
spring.activemq.brokerUrl=tcp://localhost:61616
# This property is required if you want Spring Boot to auto-define the ActiveMQConnectionFactory,
# otherwise you can define that bean in Spring config
spring.jms.cache.enabled=false

# HTTP Basic Authentication that will be used by the API
content.service.security.basicAuth.username=admin
content.service.security.basicAuth.password=admin
# Location of the server and API endpoints
content.service.url=http://localhost:8080
content.service.path=/alfresco/api/-default-/public/alfresco/versions/1
search.service.path=/alfresco/api/-default-/public/search/versions/1
Note: The configuration will look slightly different if you want to use the Spring Integration for event handling.
  1. Add the following dependencies in the Maven project file (i.e. pom.xml):
     <dependencies>
         <!-- Alfresco Java SDK 6 Java Event Handler API Spring Boot Starter -->
         <dependency>
             <groupId>org.alfresco</groupId>
             <artifactId>alfresco-java-event-api-spring-boot-starter</artifactId>
             <version>6.2.0</version>
         </dependency>
            
         <!-- Alfresco Java SDK 6 Java ReST API wrapper Spring Boot Starter -->
         <dependency>
         <groupId>org.alfresco</groupId>
         <artifactId>alfresco-acs-java-rest-api-spring-boot-starter</artifactId>
         <version>6.2.0</version>
         </dependency>
     </dependencies>
    
  2. Remove the default Spring Boot starter dependency (i.e. <artifactId>spring-boot-starter</artifactId>).
  3. Modify the contents of the Spring Boot application class (org/alfresco/tutorial/sdk5demo/Sdk5DemoApplication.java) by adding the following com.fasterxml.jackson.databind.ObjectMapper. This is required for deserializing dates:
     package org.alfresco.tutorial.sdk5demo;
    
     import com.fasterxml.jackson.databind.ObjectMapper;
     import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.boot.SpringApplication;
     import org.springframework.boot.autoconfigure.SpringBootApplication;
    
     import javax.annotation.PostConstruct;
        
     @SpringBootApplication
     public class Sdk5DemoApplication {
        
         @Autowired
         private ObjectMapper objectMapper;
        
         @PostConstruct
         public void setUp() {
             objectMapper.registerModule(new JavaTimeModule());
         }
        
         public static void main(String[] args) {
             SpringApplication.run(Sdk5DemoApplication.class, args);
         }
     }
    
  4. Test it:
     $ mvn spring-boot:run -Dlicense.skip=true
     ...
     2021-04-08 15:21:17.392  INFO 63958 --- [           main] o.a.t.sdk5demo.Sdk5DemoApplication       : Started Sdk5DemoApplication in 2.531 seconds (JVM running for 3.0)
    

    We can now add event handling and ReST API code. Here is an example of an event handler that is triggered when a file is uploaded. It then calls back to the repository via the ReST API to get the file content:

    package org.alfresco.tutorial.sdk5demo;
    
    import org.alfresco.core.handler.NodesApi;
    import org.alfresco.event.sdk.handling.filter.*;
    import org.alfresco.event.sdk.handling.handler.OnNodeCreatedEventHandler;
    import org.alfresco.event.sdk.model.v1.model.DataAttributes;
    import org.alfresco.event.sdk.model.v1.model.NodeResource;
    import org.alfresco.event.sdk.model.v1.model.RepoEvent;
    import org.alfresco.event.sdk.model.v1.model.Resource;
    import org.apache.commons.io.IOUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.io.InputStream;
    import java.nio.charset.StandardCharsets;
    
    /**
     * Sample event handler to demonstrate reacting to a document/file being uploaded to the repository.
     * It then uses the Java ReST API to call back for the file content from the repository.
     */
    @Component
    public class ContentUploadedEventHandler implements OnNodeCreatedEventHandler {
        private static final Logger LOGGER = LoggerFactory.getLogger(ContentUploadedEventHandler.class);
    
        @Autowired
        NodesApi nodesApi;
    
        public void handleEvent(final RepoEvent<DataAttributes<Resource>> repoEvent) {
            NodeResource nodeResource = (NodeResource) repoEvent.getData().getResource();
            LOGGER.info("A file was uploaded to the repository: {}, {}, {}", nodeResource.getId(), nodeResource.getNodeType(),
                    nodeResource.getName());
            try {
                InputStream fileInputStream = nodesApi.getNodeContent(
                        nodeResource.getId(), true, null, null)
                        .getBody()
                        .getInputStream();
    
                String result = IOUtils.toString(fileInputStream, StandardCharsets.UTF_8.toString());
                LOGGER.info("A file '{}' was uploaded with the following content: {}", nodeResource.getName(), result);
    
            } catch (Exception ex) {
                LOGGER.error("An error occurred trying to download the content of the file", ex);
            }
        }
    
        public EventFilter getEventFilter() {
            return IsFileFilter.get();
        }
    }
    

    Running this and uploading a text file to the Repository gives logging as follows:

    $ mvn spring-boot:run -Dlicense.skip=true
    ...
    2021-04-08 15:36:46.441  INFO 64121 --- [erContainer#0-1] o.a.t.s.ContentUploadedEventHandler      : A file was uploaded to the repository: 9e99d999-ef8f-4f6f-9582-ac5f52c2bf8d, cm:content, some.txt
    2021-04-08 15:36:47.134  INFO 64121 --- [erContainer#0-1] o.a.t.s.ContentUploadedEventHandler      : A file 'some.txt' was uploaded with the following content: This