Spring integration Spring集成Java DSL+;活动

Spring integration Spring集成Java DSL+;活动,spring-integration,activiti,spring-amqp,spring-rabbit,Spring Integration,Activiti,Spring Amqp,Spring Rabbit,我看了关于Spring+Activiti的视频。 我需要将进程立即移动到serviceTask,Activiti在这里调用我的网关,向队列(rabbitMQ)发送请求消息。发送请求消息后,进程停止。然后,只要响应消息在响应队列中,serviceTask就会再次启动。 serviceTask可能需要很长时间 我尝试了网络研讨会上的例子,效果很好,但是同步 这是我的ActivityDemoApplication.java package com.example; import java.util.

我看了关于Spring+Activiti的视频。 我需要将进程立即移动到
serviceTask
,Activiti在这里调用我的网关,向队列(rabbitMQ)发送请求消息。发送请求消息后,进程停止。然后,只要响应消息在响应队列中,
serviceTask
就会再次启动。
serviceTask
可能需要很长时间

我尝试了网络研讨会上的例子,效果很好,但是同步

这是我的ActivityDemoApplication.java

package com.example;

import java.util.Map;

import javax.sql.DataSource;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.pvm.delegate.ActivityExecution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.spring.integration.ActivitiInboundGateway;
import org.activiti.spring.integration.IntegrationActivityBehavior;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.integration.activiti.gateway.AsyncActivityBehaviorMessagingGateway;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.support.GenericHandler;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Configuration
@ComponentScan
// @EnableAutoConfiguration
@SpringBootApplication
public class ActivitiDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ActivitiDemoApplication.class, args);
    }

    @Bean
    IntegrationActivityBehavior activitiDelegate(
            ActivitiInboundGateway activitiInboundGateway) {
        return new IntegrationActivityBehavior(activitiInboundGateway);
    }

    @Bean
    ActivitiInboundGateway inboundGateway(ProcessEngine processEngine) {
        return new ActivitiInboundGateway(processEngine, "processed");
    }

    @Bean
    IntegrationFlow inboundProcess(
            ActivitiInboundGateway activitiInboundGateway,
            PhotoService photoService) {
        return IntegrationFlows.from(activitiInboundGateway)
                .handle(new GenericHandler<ActivityExecution>() {
                    @Override
                    public Object handle(ActivityExecution execution,
                            Map<String, Object> headers) {
                        try {
                            photoService.Execute();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        return MessageBuilder.withPayload(execution)
                                .setHeader("processed", (Object) true)
                                .copyHeaders(headers).build();
                    }
                }).get();
    }

    @Bean
    public DataSource database() {
        return DataSourceBuilder
                .create()
                .url("jdbc:sqlserver://localhost:1433;databaseName=activiti")
                .username("activiti")
                .password("activiti")
                .driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
                .build();
    }

    @RestController
    public static class MyRestController {
        @Autowired
        private RuntimeService runtimeService;

        @RequestMapping(value = "/start-my-process", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
        public void startMyProcess() {
            ProcessInstance p = runtimeService
                    .startProcessInstanceByKey("TestProcess");
            System.out.println("id: " + p.getId());
            System.out.println("count: "
                    + runtimeService.createProcessInstanceQuery().count());
        }

    }
}

@Service
@Transactional
class PhotoService {
    @Autowired
    private TaskService taskService;

    public void Execute() throws InterruptedException {
        System.out.println("debug 1");
        Thread.currentThread().sleep(2000);
        System.out.println("debug 2");
    }
}
我不知道如何使用网关实现amqp

    @Bean
    IntegrationActivityBehavior activitiDelegate(
            ActivitiInboundGateway activitiInboundGateway) {
        return new IntegrationActivityBehavior(activitiInboundGateway);
    }

    @Bean
    ActivitiInboundGateway inboundGateway(ProcessEngine processEngine) {
        return new ActivitiInboundGateway(processEngine, "processed");
    }

    @Bean
    IntegrationFlow inboundProcess(
            ActivitiInboundGateway activitiInboundGateway,
            PhotoService photoService) {
        return IntegrationFlows.from(activitiInboundGateway)
                .handle(new GenericHandler<ActivityExecution>() {
                    @Override
                    public Object handle(ActivityExecution execution,
                            Map<String, Object> headers) {
                        try {
                            photoService.Execute();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        return MessageBuilder.withPayload(execution)
                                .setHeader("processed", (Object) true)
                                .copyHeaders(headers).build();
                    }
                }).get();
    }
@Bean
集成活动行为活动删除门(
ActivitiInboundGateway ActivitiInboundGateway){
返回新的IntegrationActivityBehavior(activitiInboundGateway);
}
@豆子
活动inboundGateway inboundGateway(ProcessEngine ProcessEngine){
返回新的ActivitiInboundGateway(processEngine,“已处理”);
}
@豆子
积分流边界过程(
ActivityInboundGateway ActivityInboundGateway,
照片服务(照片服务){
返回IntegrationFlows.from(activitiInboundGateway)
.handle(新的GenericHandler(){
@凌驾
公共对象句柄(ActivityExecution,
地图标题){
试一试{
photoService.Execute();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回MessageBuilder.withPayload(执行)
.setHeader(“已处理”,(对象)true)
.copyHeaders(headers.build();
}
}).get();
}

很抱歉,我是JavaSpring集成的begginer。我希望我的解释清楚。

对不起,你的问题不清楚,特别是对于那些不熟悉上述框架之一的人

您说的是
serviceTask
,但不在代码中显示。我只能猜你提到的是
photoService
,它在下面的
IntegrationFlow
中没有被调用。这也让人更加困惑

我只能说,
activiinboundgateway
阻塞的组件(请参见
activiinboundgateway.execute()
):

看起来您可以扩展
IntegrationActivityBehavior

public class AsyncIntegrationActivityBehavior extends IntegrationActivityBehavior {
   
   private TaskExecutor executor = new ThreadPoolTaskExecutor();

    @Override
    public void execute(final ActivityExecution execution) throws Exception {
        this.executor(new Runnable() {

                  public void void run() {
                     AsyncIntegrationActivityBehavior.super.execute(execution);
                       }
               });
    }

}

并将其用于您的
activitiDelegate
@Bean

谢谢您的回复。你是对的。ActivitiInboundGateway已被阻止。还有别的办法吗?serviceTask只发送请求消息,进程立即停止。然后一些接收者用响应消息再次启动进程。我不知道我是否能在spring积分中解决它,还是只在bpmn activiti中解决它。例如,来自serviceTask的请求消息只能在夜间处理中处理。请参阅我的答案的更新。
return MessageBuilder.withPayload(execution).setHeader("processed", (Object) true).copyHeaders(headers).build();
    @Bean
    IntegrationActivityBehavior activitiDelegate(
            ActivitiInboundGateway activitiInboundGateway) {
        return new IntegrationActivityBehavior(activitiInboundGateway);
    }

    @Bean
    ActivitiInboundGateway inboundGateway(ProcessEngine processEngine) {
        return new ActivitiInboundGateway(processEngine, "processed");
    }

    @Bean
    IntegrationFlow inboundProcess(
            ActivitiInboundGateway activitiInboundGateway,
            PhotoService photoService) {
        return IntegrationFlows.from(activitiInboundGateway)
                .handle(new GenericHandler<ActivityExecution>() {
                    @Override
                    public Object handle(ActivityExecution execution,
                            Map<String, Object> headers) {
                        try {
                            photoService.Execute();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        return MessageBuilder.withPayload(execution)
                                .setHeader("processed", (Object) true)
                                .copyHeaders(headers).build();
                    }
                }).get();
    }
Message<?> reply = sendAndReceiveMessage(mb.build());
 <serviceTask id="task-integration" name="task integration" activiti:delegateExpression="#{activitiDelegate}"/>
public class AsyncIntegrationActivityBehavior extends IntegrationActivityBehavior {
   
   private TaskExecutor executor = new ThreadPoolTaskExecutor();

    @Override
    public void execute(final ActivityExecution execution) throws Exception {
        this.executor(new Runnable() {

                  public void void run() {
                     AsyncIntegrationActivityBehavior.super.execute(execution);
                       }
               });
    }

}