• 沒有找到結果。

Spring-Aware Workers

在文檔中 AWS Flow Framework for Java (頁 99-103)

When using Spring, you should use the Spring-aware worker classes provided by the framework:

SpringWorkflowWorker and SpringActivityWorker. These workers can be injected in your application using Spring as shown in the next example. The Spring-aware workers implement Spring's SmartLifecycle interface and, by default, automatically start polling for tasks when the Spring context is initialized. You can turn off this functionality by setting the disableAutoStartup property of the worker to true.

The following example shows how to configure a decider. This example uses MyActivities and MyWorkflow interfaces (not shown here) and corresponding implementations, MyActivitiesImpl and MyWorkflowImpl. The generated client interfaces and implementations are MyWorkflowClient/MyWorkflowClientImpl and

Spring Integration

The activities client is injected in the workflow implementation using Spring's auto wire feature:

public class MyWorkflowImpl implements MyWorkflow { @Autowired

public MyActivitiesClient client;

@Override

public void start() { client.activity1();

} }

The Spring configuration for the decider is as follows:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://

www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/

spring-aop-2.5.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- register custom workflow scope -->

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">

<property name="scopes">

<map>

<entry key="workflow">

<bean class="com.amazonaws.services.simpleworkflow.flow.spring.WorkflowScope" />

</entry>

</map>

</property>

</bean>

<context:annotation-config/>

<bean id="accesskeys" class="com.amazonaws.auth.BasicAWSCredentials">

<constructor-arg value="{AWS.Access.ID}"/>

<constructor-arg value="{AWS.Secret.Key}"/>

</bean>

<bean id="clientConfiguration" class="com.amazonaws.ClientConfiguration">

<property name="socketTimeout" value="70000" />

</bean>

<!-- Amazon SWF client -->

<bean id="swfClient"

class="com.amazonaws.services.simpleworkflow.AmazonSimpleWorkflowClient">

<constructor-arg ref="accesskeys" />

<constructor-arg ref="clientConfiguration" />

<property name="endpoint" value="{service.url}" />

</bean>

<!-- activities client -->

<bean id="activitiesClient" class="aws.flow.sample.MyActivitiesClientImpl"

scope="workflow">

</bean>

<!-- workflow implementation -->

<bean id="workflowImpl" class="aws.flow.sample.MyWorkflowImpl" scope="workflow">

<property name="client" ref="activitiesClient"/>

Spring Integration

</bean>

<!-- workflow worker -->

<bean id="workflowWorker"

class="com.amazonaws.services.simpleworkflow.flow.spring.SpringWorkflowWorker">

<constructor-arg ref="swfClient" />

<constructor-arg value="domain1" />

<constructor-arg value="tasklist1" />

<property name="registerDomain" value="true" />

<property name="domainRetentionPeriodInDays" value="1" />

<property name="workflowImplementations">

<list>

Since the SpringWorkflowWorker is fully configured in Spring and automatically starts polling when the Spring context is initialized, the host process for the decider is simple:

public class WorkflowHost {

public static void main(String[] args){

ApplicationContext context

= new FileSystemXmlApplicationContext("resources/spring/WorkflowHostBean.xml");

System.out.println("Workflow worker started");

} }

Similarly, the activity worker can be configured as follows:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://

www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/

spring-aop-2.5.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- register custom scope -->

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">

<property name="scopes">

<map>

<entry key="workflow">

<bean

class="com.amazonaws.services.simpleworkflow.flow.spring.WorkflowScope" /

>

</entry>

</map>

</property>

</bean>

<bean id="accesskeys" class="com.amazonaws.auth.BasicAWSCredentials">

<constructor-arg value="{AWS.Access.ID}"/>

<constructor-arg value="{AWS.Secret.Key}"/>

</bean>

Spring Integration

</bean>

<!-- Amazon SWF client -->

<bean id="swfClient"

class="com.amazonaws.services.simpleworkflow.AmazonSimpleWorkflowClient">

<constructor-arg ref="accesskeys" />

<constructor-arg ref="clientConfiguration" />

<property name="endpoint" value="{service.url}" />

</bean>

<!-- activities impl -->

<bean name="activitiesImpl" class="asadj.spring.test.MyActivitiesImpl">

</bean>

<!-- activity worker -->

<bean id="activityWorker"

class="com.amazonaws.services.simpleworkflow.flow.spring.SpringActivityWorker">

<constructor-arg ref="swfClient" />

<constructor-arg value="domain1" />

<constructor-arg value="tasklist1" />

<property name="registerDomain" value="true" />

<property name="domainRetentionPeriodInDays" value="1" />

<property name="activitiesImplementations">

<list>

The activity worker host process is similar to the decider:

public class ActivityHost {

public static void main(String[] args) {

ApplicationContext context = new FileSystemXmlApplicationContext(

"resources/spring/ActivityHostBean.xml");

System.out.println("Activity worker started");

} }

Injecting Decision Context

If your workflow implementation depends on the context objects, then you can easily inject them through Spring as well. The framework automatically registers context-related beans in the Spring container. For example, in the following snippet, the various context objects have been auto wired. No other Spring configuration of the context objects is required.

public class MyWorkflowImpl implements MyWorkflow { @Autowired

public MyActivitiesClient client;

@Autowired

public WorkflowClock clock;

@Autowired

public DecisionContext dcContext;

@Autowired

public GenericActivityClient activityClient;

@Autowired

public GenericWorkflowClient workflowClient;

@Autowired

public WorkflowContext wfContext;

@Override

在文檔中 AWS Flow Framework for Java (頁 99-103)

相關文件