use of org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity in project Activiti by Activiti.
the class EventSubscriptionQueryTest method testQueryByActivityId.
public void testQueryByActivityId() {
processEngineConfiguration.getCommandExecutor().execute(new Command<Void>() {
public Void execute(CommandContext commandContext) {
MessageEventSubscriptionEntity messageEventSubscriptionEntity1 = new MessageEventSubscriptionEntity();
messageEventSubscriptionEntity1.setEventName("messageName");
messageEventSubscriptionEntity1.setActivityId("someActivity");
messageEventSubscriptionEntity1.insert();
MessageEventSubscriptionEntity messageEventSubscriptionEntity2 = new MessageEventSubscriptionEntity();
messageEventSubscriptionEntity2.setEventName("messageName");
messageEventSubscriptionEntity2.setActivityId("someActivity");
messageEventSubscriptionEntity2.insert();
SignalEventSubscriptionEntity signalEventSubscriptionEntity3 = new SignalEventSubscriptionEntity();
signalEventSubscriptionEntity3.setEventName("messageName2");
signalEventSubscriptionEntity3.setActivityId("someOtherActivity");
signalEventSubscriptionEntity3.insert();
return null;
}
});
List<EventSubscriptionEntity> list = newEventSubscriptionQuery().activityId("someOtherActivity").list();
assertEquals(1, list.size());
list = newEventSubscriptionQuery().activityId("someActivity").eventType("message").list();
assertEquals(2, list.size());
cleanDb();
}
use of org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity in project Activiti by Activiti.
the class BpmnDeployer method addMessageEventSubscriptions.
@SuppressWarnings("unchecked")
protected void addMessageEventSubscriptions(ProcessDefinitionEntity processDefinition) {
CommandContext commandContext = Context.getCommandContext();
List<EventSubscriptionDeclaration> eventDefinitions = (List<EventSubscriptionDeclaration>) processDefinition.getProperty(BpmnParse.PROPERTYNAME_EVENT_SUBSCRIPTION_DECLARATION);
if (eventDefinitions != null) {
Set<String> messageNames = new HashSet<String>();
for (EventSubscriptionDeclaration eventDefinition : eventDefinitions) {
if (eventDefinition.getEventType().equals("message") && eventDefinition.isStartEvent()) {
if (!messageNames.contains(eventDefinition.getEventName())) {
messageNames.add(eventDefinition.getEventName());
} else {
throw new ActivitiException("Cannot deploy process definition '" + processDefinition.getResourceName() + "': there are multiple message event subscriptions for the message with name '" + eventDefinition.getEventName() + "'.");
}
// look for subscriptions for the same name in db:
List<EventSubscriptionEntity> subscriptionsForSameMessageName = commandContext.getEventSubscriptionEntityManager().findEventSubscriptionsByName(MessageEventHandler.EVENT_HANDLER_TYPE, eventDefinition.getEventName(), processDefinition.getTenantId());
// also look for subscriptions created in the session:
List<MessageEventSubscriptionEntity> cachedSubscriptions = commandContext.getDbSqlSession().findInCache(MessageEventSubscriptionEntity.class);
for (MessageEventSubscriptionEntity cachedSubscription : cachedSubscriptions) {
if (eventDefinition.getEventName().equals(cachedSubscription.getEventName()) && !subscriptionsForSameMessageName.contains(cachedSubscription)) {
subscriptionsForSameMessageName.add(cachedSubscription);
}
}
// remove subscriptions deleted in the same command
subscriptionsForSameMessageName = commandContext.getDbSqlSession().pruneDeletedEntities(subscriptionsForSameMessageName);
for (EventSubscriptionEntity eventSubscriptionEntity : subscriptionsForSameMessageName) {
// no process instance-id = it's a message start event
if (StringUtils.isEmpty(eventSubscriptionEntity.getProcessInstanceId())) {
throw new ActivitiException("Cannot deploy process definition '" + processDefinition.getResourceName() + "': there already is a message event subscription for the message with name '" + eventDefinition.getEventName() + "'.");
}
}
MessageEventSubscriptionEntity newSubscription = new MessageEventSubscriptionEntity();
newSubscription.setEventName(eventDefinition.getEventName());
newSubscription.setActivityId(eventDefinition.getActivityId());
newSubscription.setConfiguration(processDefinition.getId());
newSubscription.setProcessDefinitionId(processDefinition.getId());
if (processDefinition.getTenantId() != null) {
newSubscription.setTenantId(processDefinition.getTenantId());
}
newSubscription.insert();
}
}
}
}
use of org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity in project Activiti by Activiti.
the class MessageThrowingEventListener method onEvent.
@Override
public void onEvent(ActivitiEvent event) {
if (isValidEvent(event)) {
if (event.getProcessInstanceId() == null) {
throw new ActivitiIllegalArgumentException("Cannot throw process-instance scoped message, since the dispatched event is not part of an ongoing process instance");
}
EventSubscriptionEntityManager eventSubscriptionEntityManager = Context.getCommandContext().getEventSubscriptionEntityManager();
List<MessageEventSubscriptionEntity> subscriptionEntities = eventSubscriptionEntityManager.findMessageEventSubscriptionsByProcessInstanceAndEventName(event.getProcessInstanceId(), messageName);
for (EventSubscriptionEntity messageEventSubscriptionEntity : subscriptionEntities) {
eventSubscriptionEntityManager.eventReceived(messageEventSubscriptionEntity, null, false);
}
}
}
use of org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity in project Activiti by Activiti.
the class ScopeUtil method throwCompensationEvent.
/**
* we create a separate execution for each compensation handler invocation.
*/
public static void throwCompensationEvent(List<CompensateEventSubscriptionEntity> eventSubscriptions, DelegateExecution execution, boolean async) {
ExecutionEntityManager executionEntityManager = Context.getCommandContext().getExecutionEntityManager();
// first spawn the compensating executions
for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
ExecutionEntity compensatingExecution = null;
// where the compensating execution is created when leaving the subprocess and holds snapshot data).
if (eventSubscription.getConfiguration() != null) {
compensatingExecution = executionEntityManager.findById(eventSubscription.getConfiguration());
compensatingExecution.setParent(compensatingExecution.getProcessInstance());
compensatingExecution.setEventScope(false);
} else {
compensatingExecution = executionEntityManager.createChildExecution((ExecutionEntity) execution);
eventSubscription.setConfiguration(compensatingExecution.getId());
}
}
// signal compensation events in reverse order of their 'created' timestamp
Collections.sort(eventSubscriptions, new Comparator<EventSubscriptionEntity>() {
public int compare(EventSubscriptionEntity o1, EventSubscriptionEntity o2) {
return o2.getCreated().compareTo(o1.getCreated());
}
});
for (CompensateEventSubscriptionEntity compensateEventSubscriptionEntity : eventSubscriptions) {
Context.getCommandContext().getEventSubscriptionEntityManager().eventReceived(compensateEventSubscriptionEntity, null, async);
}
}
use of org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity in project Activiti by Activiti.
the class EventSubscriptionQueryTest method testQueryByExecutionId.
@Deployment
public void testQueryByExecutionId() {
// starting two instances:
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("catchSignal");
runtimeService.startProcessInstanceByKey("catchSignal");
// test query by process instance id
EventSubscriptionEntity subscription = newEventSubscriptionQuery().processInstanceId(processInstance.getId()).singleResult();
assertThat(subscription).isNotNull();
Execution executionWaitingForSignal = runtimeService.createExecutionQuery().activityId("signalEvent").processInstanceId(processInstance.getId()).singleResult();
// test query by execution id
EventSubscriptionEntity signalSubscription = newEventSubscriptionQuery().executionId(executionWaitingForSignal.getId()).singleResult();
assertThat(signalSubscription).isNotNull();
assertThat(subscription).isEqualTo(signalSubscription);
cleanDb();
}
Aggregations