This event is fired whenever a peer association is created, such as via the the POST nodes/{sourceId}/targets. For more information, see Create an Association for a Node that Exists.
ReST API. The full name of this event is org.alfresco.event.assoc.peer.Created.
Here is an example payload for this event type:
{ "specversion": "1.0", "type": "org.alfresco.event.assoc.peer.Created", "id": "8a8113a2-fa67-4914-9ecb-2ec47c456159", "source": "/08d9b620-48de-4247-8f33-360988d3b19b", "time": "2021-01-28T13:42:34.352956Z", "dataschema": "https://api.alfresco.com/schema/event/repo/v1/peerAssocCreated", "datacontenttype": "application/json", "data": { "eventGroupId": "78da21cc-fa5a-47d1-afcb-03005229efa9", "resource": { "@type": "PeerAssociationResource", "assocType": "fdk:reviews", "source": { "id": "a4eb7684-0ffe-4bf5-b6f7-4297a6e4ee84" }, "target": { "id": "f826ac49-0262-48af-8f63-f87eb7007078" } } } }
Using the Node Browser (see Using the Node Browser) the following NodeRefs were resolved as follows:
"source": { "id": "a4eb7684-0ffe-4bf5-b6f7-4297a6e4ee84" /app:company_home/cm:My_x0020_Gadgets/cm:My_x0020_Gadget }, "target": { "id": "f826ac49-0262-48af-8f63-f87eb7007078" /app:company_home/cm:My_x0020_Gadgets/cm:gadget-review.txt
The event payload is telling us that a peer association of type fdk:reviews (i.e. data.resource.assocType) was set up between a gadget file My Gadget (i.e. data.resource.source) and a gadget review gadget-review.txt (i.e. data.resource.target).
When subscribing to the org.alfresco.event.assoc.peer.Created event it’s possible to filter out anything that is of no interest. So for example, if you are only interested in associations of type fdk:reviews it would be easy to configure this.
SDK5 - Plain Java
The following code shows how this can be done with SDK 5 and plain Java event handlers:
import org.alfresco.event.sdk.handling.filter.AssocTypeFilter; import org.alfresco.event.sdk.handling.filter.EventFilter; import org.alfresco.event.sdk.handling.handler.OnPeerAssocCreatedEventHandler; import org.alfresco.event.sdk.model.v1.model.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; /** * Sample event handler to demonstrate reacting to a peer-2-peer assoc being created. */ @Component public class Peer2PeerAssocCreatedEventHandler implements OnPeerAssocCreatedEventHandler { private static final Logger LOGGER = LoggerFactory.getLogger(Peer2PeerAssocCreatedEventHandler.class); public void handleEvent(final RepoEvent<DataAttributes<Resource>> repoEvent) { PeerAssociationResource resource = (PeerAssociationResource) repoEvent.getData().getResource(); LOGGER.info("A Peer-Peer association was created: Source {} -> Target {}", resource.getSource().getId(), resource.getTarget().getId()); } public EventFilter getEventFilter() { return AssocTypeFilter.of("fdk:reviews"); // Make sure the Peer-Peer association is of type FDK Reviews } }
This code uses the org.alfresco.event.sdk.handling.filter.AssocTypeFilter event filter to specify what type of Peer-2-Peer association we are interested in.
For more information about how to extract all the properties from the message payload see the PeerAssociationResource information in Software Development Kits (SDK).
To create an SDK event handler project that uses plain Java event handlers follow the instructions on pure Java event handlers in Software Development Kits (SDK).
SDK5 - Spring Integration
The following code shows how this can be done with SDK 5 and Spring Integration event handlers:
package org.alfresco.tutorial.events; import org.alfresco.event.sdk.handling.filter.AssocTypeFilter; import org.alfresco.event.sdk.handling.filter.EventTypeFilter; import org.alfresco.event.sdk.integration.EventChannels; import org.alfresco.event.sdk.integration.filter.IntegrationEventFilter; import org.alfresco.event.sdk.model.v1.model.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.integration.dsl.IntegrationFlowAdapter; import org.springframework.integration.dsl.IntegrationFlowDefinition; import org.springframework.messaging.Message; import org.springframework.stereotype.Component; /** * Spring Integration based event handler that will execute code when a peer-2-peer assoc is being created. */ @Component public class Peer2PeerAssocCreatedFlow extends IntegrationFlowAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(Peer2PeerAssocCreatedFlow.class); // Use builder to create an integration flow based on alfresco.events.main.channel event channel @Override protected IntegrationFlowDefinition<?> buildFlow() { return from(EventChannels.MAIN) // Listen to events coming from the Alfresco events channel .filter(IntegrationEventFilter.of(EventTypeFilter.PEER_ASSOC_CREATED)) // Filter events and select only Peer2Peer assoc created events .filter(IntegrationEventFilter.of(AssocTypeFilter.of("fdk:reviews"))) // Make sure the Peer2Peer association is of type FDK Reviews .handle(t -> handleEvent(t)); // Handle event with a bit of logging } private void handleEvent(Message message) { RepoEvent<DataAttributes<Resource>> repoEvent = (RepoEvent<DataAttributes<Resource>>)message.getPayload(); PeerAssociationResource resource = (PeerAssociationResource) repoEvent.getData().getResource(); LOGGER.info("A Peer-Peer association was created: Source {} -> Target {}", resource.getSource().getId(), resource.getTarget().getId()); } }
This code uses the org.alfresco.event.sdk.handling.filter.AssocTypeFilter event filter to specify what type of Peer-2-Peer association we are interested in.
For more information about how to extract all the properties from the message payload see the PeerAssociationResource information in Software Development Kits (SDK).
To create an SDK event handler project that uses plain Java event handlers follow the instructions on pure Java event handlers in Software Development Kits (SDK).
Apache Camel
The following code snippet shows how this could be done with an Apache Camel route configuration:
public class SimpleRoute extends RouteBuilder { @Override public void configure() { from("amqpConnection:topic:alfresco.repo.event2") .id("PeerAssocCreatedRoute") .log("${body}") // Log all incoming events on this topic, even those that we are not interested in .choice() .when() // When the following is true: // The event type is peer assoc created .jsonpath("$[?(@.type=='org.alfresco.event.assoc.peer.Created' && " + // and the association type is fdk:reviews "@.data.resource.assocType=='fdk:reviews')]" ) // Unpack the data into JSON format .unmarshal("publicDataFormat") // Call a Spring Bean with the event data .bean("peerAssocCreatedEventHandlerImpl", "onReceive(*, COPY)") .end(); } }
The jsonpath expression uses several of the event data properties to filter out exactly the events we are interested in.
In this case a Spring Bean with ID peerAssocCreatedEventHandlerImpl is called at the end of the route from where you could make the necessary ReST API calls.