Flowable事件-消息事件

Flowable事件-消息事件
在这里插入图片描述

Flowable消息事件

  消息事件(message event),是指引用具名消息的事件。消息具有名字与载荷。与信号不同,消息事件只有一个接收者

1.启动事件

  消息启动事件,也就是我们通过接收到某些消息后来启动流程实例,比如接收到了一封邮件,一条短信等,具体通过案例来讲解

Flowable事件-消息事件
在这里插入图片描述

我们需要先定义一个消息

Flowable事件-消息事件
在这里插入图片描述

然后在消息开始节点出引用

Flowable事件-消息事件
在这里插入图片描述

然后通过代码来处理,部署和启动

    /**
     * Deploy
     */

    @Test
    void testDeploy() throws Exception {
        //RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("消息启动事件.bpmn20.xml")
                .name("消息启动事件")
                .deploy();
        System.out.println("deploy.getId() = " + deploy.getId());
        System.out.println("deploy.getName() = " + deploy.getName());
        System.out.println("部署开始的时间:" + new Date());
        //TimeUnit.MINUTES.sleep(3);
    }

部署后不会自动启动,我们需要接收相关的信息后来触发。

   /**
     * 通过消息来启动一个流程实例
     */

    @Test
    void startFlow() throws Exception{

       // runtimeService.startProcessInstanceById("event006:1:0532e730-af02-11ec-8cf3-c03c59ad2248");
       // 注意:发送消息发送的应该是消息的名称而不是消息的ID
        runtimeService.startProcessInstanceByMessage("第一个消息");
        System.out.println("启动时间:" + new Date());
        // 我们得保证容器的运行,所以需要阻塞
        TimeUnit.MINUTES.sleep(1);
    }

注意:发送消息发送的应该是消息的名称而不是消息的ID,报错如下:Flowable事件-消息事件

2.中间事件

  消息中间事件就是在流程运作中需要消息来触发的场景,案例演示,自动流程1处理完成后,需要接收特定的消息之后才能进入到自动流程2

Flowable事件-消息事件
在这里插入图片描述

消息中间事件绑定的消息为

Flowable事件-消息事件
在这里插入图片描述

然后通过代码来演示

   /**
     * Deploy
     */

    @Test
    void testDeploy() throws Exception {
        //RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("消息中间事件.bpmn20.xml")
                .name("消息中间事件")
                .deploy();
        System.out.println("deploy.getId() = " + deploy.getId());
        System.out.println("deploy.getName() = " + deploy.getName());
        System.out.println("部署开始的时间:" + new Date());
        //TimeUnit.MINUTES.sleep(3);
    }

    /**
     * 通过消息来启动一个流程实例
     */

    @Test
    void startFlow() throws Exception{

        runtimeService.startProcessInstanceById("event008:1:9217aa5e-af0e-11ec-b11f-c03c59ad2248");
       // 注意:发送消息发送的应该是消息的名称而不是消息的ID
        //runtimeService.startProcessInstanceByMessage("第一个消息");
        System.out.println("启动时间:" + new Date());
        // 我们得保证容器的运行,所以需要阻塞
        //TimeUnit.MINUTES.sleep(1);
    }

    /**
     * 中间事件-发布消息
     */

    @Test
    void recevedMsg(){
        // 需要查询到executionId
        String processExecutionId = "b5349e22-af0e-11ec-93e6-c03c59ad2248";
        // 我们需要根据流程实例编号找到对应的执行编号
       /* Execution execution = runtimeService.createExecutionQuery()
                .processInstanceId("event008:1:9217aa5e-af0e-11ec-b11f-c03c59ad2248")
                .singleResult();
        System.out.println("----------->"+execution.getId());*/

        runtimeService.messageEventReceived("第二个消息",processExecutionId);
    }

可以看到的输出效果

Flowable事件-消息事件
在这里插入图片描述

3.边界事件

  消息边界事件,如果在消息触发前还没有,案例演示:Flowable事件-消息事件

部署流程

    /**
     * Deploy
     */

    @Test
    void testDeploy() throws Exception {
        //RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("消息边界事件.bpmn20.xml")
                .name("消息边界事件")
                .deploy();
        System.out.println("deploy.getId() = " + deploy.getId());
        System.out.println("deploy.getName() = " + deploy.getName());
        System.out.println("部署开始的时间:" + new Date());
        //TimeUnit.MINUTES.sleep(3);
    }
    /**
     * 通过消息来启动一个流程实例
     */

    @Test
    void startFlow() throws Exception{

        runtimeService.startProcessInstanceById("event009:1:f2096787-af11-11ec-b290-c03c59ad2248");
       // 注意:发送消息发送的应该是消息的名称而不是消息的ID
        //runtimeService.startProcessInstanceByMessage("第一个消息");
        System.out.println("启动时间:" + new Date());
        // 我们得保证容器的运行,所以需要阻塞
        //TimeUnit.MINUTES.sleep(1);
    }

部署流程后启动流程实例会运转到

Flowable事件-消息事件
在这里插入图片描述

如果人工处理在消息订阅前没有处理就会触发边界事件

    /**
     * 边界事件-发布消息
     */

    @Test
    void recevedMsg(){
        // 需要查询到executionId
        String processExecutionId = "1d503361-af12-11ec-89a4-c03c59ad2248";
        // 我们需要根据流程实例编号找到对应的执行编号
       /* Execution execution = runtimeService.createExecutionQuery()
                .processInstanceId("event008:1:9217aa5e-af0e-11ec-b11f-c03c59ad2248")
                .singleResult();
        System.out.println("----------->"+execution.getId());*/

        runtimeService.messageEventReceived("第三个消息",processExecutionId);
    }
Flowable事件-消息事件
在这里插入图片描述

搞定~


原文始发于微信公众号(波哥带你学编程):Flowable事件-消息事件

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/58532.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!