cancel
Showing results for 
Search instead for 
Did you mean: 

activity - get service registry in java class

dmralfing
Champ in-the-making
Champ in-the-making
Somebody could tell me what I´m doing wrong to get the service registry in the java class, or
well any way to get it? - I have tryed using the solution I read in the forum topic :
https://forums.alfresco.com/forum/developer-discussions/workflow/proper-way-implement-custom-tasklis...
..but I´m afraid I´m doing something wrong due to I´m a beginer in activity, so I would apreciate if somebody help me..
This is the Java code:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="Examples">
  <process id="MyTest" name="MyTest">
   <startEvent id="theStart" name="Start">
   </startEvent>
    <serviceTask id="javaService" name="Java service invocation" activiti:class="org.activiti.examples.bpmn.test.manager2.SetterTest">
      <extensionElements>
        <activiti:field name="text">
          <activiti:string>Success</activiti:string>
        </activiti:field>
        <activiti:taskListener event="create" class="org.activiti.examples.bpmn.test.manager2.SetterTest">
        </activiti:taskListener>
      </extensionElements>
    </serviceTask>
   <userTask id="usertask1" name="User Task" activiti:candidateGroups="user"></userTask>
   
   <endEvent id="theEnd" name="End"></endEvent>
    <sequenceFlow id="flow1" name="" sourceRef="theStart" targetRef="javaService"></sequenceFlow>
    <sequenceFlow id="flow2" name="" sourceRef="javaService" targetRef="usertask1"></sequenceFlow>   
    <sequenceFlow id="flow4" name="" sourceRef="usertask1" targetRef="theEnd"></sequenceFlow>
  </process>
</definitions>


..and this is my class that unaffortunately doesn´t get the serviceRegistry..

package org.activiti.examples.bpmn.test.manager2;
import …
……….
public class SetterTest implements JavaDelegate {

   private static final String VARIABLE_NAME = "testVariable";
   private Expression text;
   ServiceRegistry serviceRegistry=null;
   //public ServiceRegistry getServiceRegistry() {return serviceRegistry;}
   protected ServiceRegistry getServiceRegistry() {

        ProcessEngineConfigurationImpl config = Context.getProcessEngineConfiguration();
        if (config != null) {
            ServiceRegistry registry = (ServiceRegistry) config.getBeans().get(ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);
            if (registry == null) {
                throw new RuntimeException(
                            "Service-registry not present in ProcessEngineConfiguration beans, expected ServiceRegistry with key" +
                            ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);
            }
            else
               System.out.println("————— config =null");
            return registry;
        }

        throw new IllegalStateException("No ProcessEngineCOnfiguration found in active context");

    }   
   public void setServiceRegistry(ServiceRegistry serviceRegistry) {this.serviceRegistry = serviceRegistry;}
   public void execute(DelegateExecution execution)
   {
         if (serviceRegistry==null)
            System.out.println("serviceRegistry is null!!");
   }
}

Thank you so much!
6 REPLIES 6

tonyrivet
Champ in-the-making
Champ in-the-making
Hi,

I think there are several problems here.

First of all, you should use your getServiceRegistry() method. The serviceRegistry attribute you are testing is never instanciated here.
Moreover, instead of using the
activiti:class
attribute, I'll recommand you to declare your delegate as a bean, inject the services you need, and reference this bean in your serviceTask via the
activiti:delegateExpression
attribute (see the Activiti guide for more information).

Secondly, you are using the same class for your serviceTask delegate and your taskListener, which are two different things. The taskListener class should be a different one, implementing the
org.activiti.engine.delegate.TaskListener
interface.

dmralfing
Champ in-the-making
Champ in-the-making
Ok, I have done what you told me but it must be something wrong..
This is the process:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="Examples">
  <process id="certificadosExpedientesCMAE" name="CMAE 10:48 - Generación de certificados de expedientes">
   <startEvent id="theStart" name="Start">
   </startEvent>

    <serviceTask id="javaService" activiti:delegateExpression="${CreacionCertificadosExpedientes}">
    </serviceTask>

   <userTask id="usertask1" name="User Task" activiti:candidateGroups="user"></userTask>
   <endEvent id="theEnd" name="End"></endEvent>
    <sequenceFlow id="flow1" name="" sourceRef="theStart" targetRef="javaService"></sequenceFlow>
    <sequenceFlow id="flow2" name="" sourceRef="javaService" targetRef="usertask1"></sequenceFlow>   
    <sequenceFlow id="flow3" name="" sourceRef="usertask1" targetRef="theEnd"></sequenceFlow>
  </process>
</definitions>


This is the bean - the bean and the java class has same name due to I´m not sure what of them
I have to reference.

    <bean id="CreacionCertificadosExpedientes"
      class="org.activiti.colaboracion.cmae.wfs.expedientes.certificados.CreacionCertificadosExpedientes" parent="baseJavaDelegate" abstract="true" depends-on="activitiBeanRegistry">
      <property name="serviceRegistry">
             <ref bean="ServiceRegistry" />
          </property>
      </bean>

Why doesn´t it work?

tonyrivet
Champ in-the-making
Champ in-the-making
Your bean mustn't be declared as abstract to be instanciated. Moreover, the baseJavaDelegate bean already injects the serviceRegistry dependency so you don't have to do it here.
And your bean id doesn't have to begin with an uppercase letter, the activitiBeanRegistry register your bean by class name and not by bean name.

This should work better :

<bean id="creacionCertificadosExpedientes" class="org.activiti.colaboracion.cmae.wfs.expedientes.certificados.CreacionCertificadosExpedientes" parent="baseJavaDelegate">
</bean>


If it doesn't, please post your error message here.

dmralfing
Champ in-the-making
Champ in-the-making
Ok, I have done exactly how you has told me and Alfresco doesn´t start, this is the error in log:

17:13:35,500 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] Starting 'sysAdmin' subsystem, ID: [sysAdmin, default]
17:13:35,652 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] Startup of 'sysAdmin' subsystem, ID: [sysAdmin, default] complete
17:13:46,082 DEBUG [org.alfresco.repo.workflow.jbpm.JBPMTransactionTemplate] jBPM persistence service present
17:13:46,082 DEBUG [org.alfresco.repo.workflow.jbpm.JBPMTransactionTemplate] creating hibernateTemplate based on jBPM SessionFactory
17:13:46,236 DEBUG [org.alfresco.repo.workflow] Registered Workflow Component 'jbpm' (class org.alfresco.repo.workflow.jbpm.JBPMEngine)
17:13:46,245 DEBUG [org.alfresco.repo.workflow] Registered Task Component 'jbpm' (class org.alfresco.repo.workflow.jbpm.JBPMEngine)
17:13:47,199 DEBUG [org.alfresco.repo.workflow] Registered Workflow Component 'activiti' (class org.alfresco.repo.workflow.activiti.ActivitiWorkflowEngine)
17:13:47,200 DEBUG [org.alfresco.repo.workflow] Registered Task Component 'activiti' (class org.alfresco.repo.workflow.activiti.ActivitiWorkflowEngine)
17:13:48,269 WARN  [org.alfresco.util.AbstractTriggerBean] Job ehCacheTracerJob is not active
17:13:52,364 DEBUG [org.alfresco.repo.module.ModuleComponentHelper] Registered component: ModuleComponent[ module=intranetAmp, name=justicia.intranetamp.component, since=1.0, appliesFrom=1.0, appliesTo=999, onceOnly=true]
17:13:53,964 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] Stopping 'sysAdmin' subsystem, ID: [sysAdmin, default]
17:13:53,993 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] Stopped 'sysAdmin' subsystem, ID: [sysAdmin, default]
17:13:54,013 ERROR [org.springframework.web.context.ContextLoader] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'creacionCertificadosExpedientes' defined in class path resource [alfresco/module/colaboracionAmp/context/wf-certificados-expedientes-context.xml]:
Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException:
Invalid property 'beanRegistry'
of bean class
[org.activiti.colaboracion.cmae.wfs.expedientes.certificados.CreacionCertificadosExpedientes]:
Bean property 'beanRegistry' is not writable or has an invalid setter method. Does the parameter
type of the setter match the return type of the getter?

   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
   at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
   at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
   at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
   at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
   at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
   at org.alfresco.web.app.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:63)
   at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
   at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
   at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
   at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
   at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
   at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:675)
   at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:601)
   at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:502)
   at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
   at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
   at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
   at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
   at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
   at org.apache.catalina.core.StandardService.start(StandardService.java:525)
   at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
   at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
   at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.NotWritablePropertyException:
Invalid property 'beanRegistry' of bean class
[org.activiti.colaboracion.cmae.wfs.expedientes.certificados.CreacionCertificadosExpedientes]:
Bean property 'beanRegistry' is not writable or has an invalid setter method.
Does the parameter type of the setter match the return type of the getter?
   at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1024)
   at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:900)
   at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
   at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)
   … 38 more

The java class I´m using, you can see at the begining of this topic. I don´t know what´s wrong!

tonyrivet
Champ in-the-making
Champ in-the-making
As you declared your bean to have the baseJaveDelegate as parent bean, your CreacionCertificadosExpedientes class must extend the BaseJavaDelegate class. It seems it doesn't… can you confirm that ?

dmralfing
Champ in-the-making
Champ in-the-making
Ok, I have done like this and it works, thank you so much!