DefaultRegisterableItemsFactory extends SimpleRegisterableItemsFactory { @Override public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) { KieSession ksession = ((InternalRuntimeEngine) runtime).internalGetKieSession(); List<ProcessEventListener> defaultListeners = new ArrayList<ProcessEventListener>(); DeploymentDescriptor descriptor = getRuntimeManager().getDeploymentDescriptor(); if (descriptor == null) { AbstractAuditLogger logger = AuditLoggerFactory.newJPAInstance(ksession.getEnvironment()); logger.setBuilder(getAuditBuilder(runtime)); defaultListeners.add(logger); } else if (descriptor.getAuditMode() == AuditMode.JPA) { AbstractAuditLogger logger = null; if (descriptor.getPersistenceUnit().equals(descriptor.getAuditPersistenceUnit())) { logger = AuditLoggerFactory.newJPAInstance(ksession.getEnvironment()); } else { Environment env = EnvironmentFactory.newEnvironment(); env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, EntityManagerFactoryManager.get().getOrCreate(descriptor.getAuditPersistenceUnit())); logger = AuditLoggerFactory.newJPAInstance(env); } logger.setBuilder(getAuditBuilder(runtime)); defaultListeners.add(logger); } else if (descriptor.getAuditMode() == AuditMode.JMS) { try { if (jmsLogger == null) { Properties properties = loadJMSProperties(); logger.debug("Creating AsyncAuditLogProducer {}", properties); jmsLogger = AuditLoggerFactory.newJMSInstance((Map) properties); jmsLogger.setBuilder(getAuditBuilder(runtime)); } defaultListeners.add(jmsLogger); } catch (IOException e) { logger.error("Unable to load jms audit properties from {}", "/jbpm.audit.jms.properties", e); } } defaultListeners.addAll(super.getProcessEventListeners(runtime)); defaultListeners.addAll(getEventListenerFromDescriptor(runtime, ProcessEventListener.class)); return defaultListeners; }  @Override Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime); @Override List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime); @Override List<AgendaEventListener> getAgendaEventListeners(RuntimeEngine runtime); @Override List<RuleRuntimeEventListener> getRuleRuntimeEventListeners(RuntimeEngine runtime); @Override List<TaskLifeCycleEventListener> getTaskListeners(); @Override Map<String, Object> getGlobals(RuntimeEngine runtime); AuditEventBuilder getAuditBuilder(); AuditEventBuilder getAuditBuilder(RuntimeEngine engine); void setAuditBuilder(AuditEventBuilder auditBuilder);  }
@Test public void testJmsAuditCacheInstance() throws Exception { KieServices ks = KieServices.Factory.get(); ReleaseId releaseId = ks.newReleaseId("org.jbpm.test.jms", "kjar-jms-audit", "1.0.0"); DeploymentDescriptor customDescriptor = new DeploymentDescriptorImpl("org.jbpm.persistence.jpa"); customDescriptor.getBuilder() .auditMode(AuditMode.JMS); Map<String, String> resources = new HashMap<String, String>(); resources.put("src/main/resources/" + DeploymentDescriptor.META_INF_LOCATION, customDescriptor.toXml()); InternalKieModule kJar1 = createKieJar(ks, releaseId, resources); installKjar(releaseId, kJar1); RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get() .newDefaultBuilder(releaseId) .classLoader(this.getClass().getClassLoader()) .get(); manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment); assertNotNull(manager); RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get()); assertNotNull(engine); AsyncAuditLogProducer asyncAuditLogProducer = null; KieSession kieSession = engine.getKieSession(); for (ProcessEventListener listener : kieSession.getProcessEventListeners()) { if (listener instanceof AsyncAuditLogProducer) { asyncAuditLogProducer = (AsyncAuditLogProducer) listener; break; } } assertNotNull(asyncAuditLogProducer); manager.close(); manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment); assertNotNull(manager); RuntimeEngine engine2 = manager.getRuntimeEngine(ProcessInstanceIdContext.get()); KieSession kieSession2 = engine2.getKieSession(); AsyncAuditLogProducer asyncAuditLogProducer2 = null; for (ProcessEventListener listener : kieSession2.getProcessEventListeners()) { if (listener instanceof AsyncAuditLogProducer) { asyncAuditLogProducer2 = (AsyncAuditLogProducer) listener; break; } } assertNotNull(asyncAuditLogProducer2); assertEquals(asyncAuditLogProducer, asyncAuditLogProducer2); }