使用Camunda-bpm-reactor的任务分配电子邮件

时间:2018-12-17 06:59:29

标签: camunda reactor

我正在尝试使用Task Assignment Email重新实现camunda-bpm-reactor。使用Java类时,我可以发送电子邮件,但是当我尝试通过事件总线重新实现它时,它将无法正常工作。

我将所需的依赖项添加为

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>org.camunda.bpm.quickstart</groupId>
      <artifactId>camunda-quickstart-task-assignment-email</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <name>Task Assignment Email</name>

      <properties>
        <camunda.version>7.10.0</camunda.version>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <failOnMissingWebXml>false</failOnMissingWebXml>
      </properties>

      <dependencies>

        <dependency>
            <groupId>org.camunda.bpm.extension</groupId>
            <artifactId>camunda-bpm-reactor-core</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
          <groupId>org.camunda.bpm</groupId>
          <artifactId>camunda-engine</artifactId>
          <version>${camunda.version}</version>
          <scope>provided</scope>
        </dependency>

        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.0.1</version>
          <scope>provided</scope>
        </dependency>

        <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-email</artifactId>
          <version>1.2</version>
        </dependency>

        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.8.2</version>
          <scope>test</scope>
        </dependency>

        <dependency>
          <groupId>com.h2database</groupId>
          <artifactId>h2</artifactId>
          <version>1.3.171</version>
          <scope>test</scope>
        </dependency>

        <!-- redirect slf4j logging to jdk logging -->
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-jdk14</artifactId>
          <version>1.7.12</version>
          <scope>test</scope>
        </dependency>

      </dependencies>

      <repositories>
        <repository>
          <id>camunda-bpm-nexus</id>
          <name>camunda-bpm-nexus</name>
          <url>https://app.camunda.com/nexus/content/groups/public</url>
        </repository>
      </repositories>
    </project>

到项目的pom.xml文件。我创建了三个类,如下所示:

TaskAssignmentApplication.java文件:

package org.camunda.bpm.quickstart;

import org.camunda.bpm.application.ProcessApplication;
import org.camunda.bpm.application.impl.ServletProcessApplication;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngineConfiguration;
import org.camunda.bpm.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration;
import org.camunda.bpm.extension.reactor.CamundaReactor;
import org.camunda.bpm.extension.reactor.bus.CamundaEventBus;

@ProcessApplication("TaskAssignmentEmail")
public class TaskAssignmentApplication extends ServletProcessApplication {

    private static ProcessEngineConfiguration CONFIGURATION = new StandaloneInMemProcessEngineConfiguration() {
        { 
          // register the plugin
          this.getProcessEnginePlugins().add(CamundaReactor.plugin());
          this.databaseSchemaUpdate = DB_SCHEMA_UPDATE_DROP_CREATE;
          this.jobExecutorActivate = false;
          this.isDbMetricsReporterActivate = false;
        }
      };

      private static ProcessEngine processEngine;

      public static ProcessEngine processEngine() {
        if (processEngine == null) {
          processEngine = CONFIGURATION.buildProcessEngine();
        }
        return processEngine;
      }

      public static void init() {
        CamundaEventBus eventBus = CamundaReactor.eventBus();
        // create and register listeners
        new TaskCreateListener(eventBus);
        new TaskAssignmentListener();
      }
}

TaskCreateListener.java文件:

package org.camunda.bpm.quickstart;

import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.TaskListener;
import org.camunda.bpm.extension.reactor.bus.CamundaEventBus;
import org.camunda.bpm.extension.reactor.bus.CamundaSelector;

@CamundaSelector(type = "userTask", event = TaskListener.EVENTNAME_CREATE, process = "TaskAssignmentEmail")
public class TaskCreateListener implements TaskListener {

    public TaskCreateListener(CamundaEventBus eventBus) {
        eventBus.register(this);
      }

    @Override
    public void notify(DelegateTask delegateTask) {
        delegateTask.setPriority(97);
        delegateTask.addCandidateGroup("IT");
    }

}

和TaskAssignmentListener.java文件:

package org.camunda.bpm.quickstart;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.SimpleEmail;
import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.TaskListener;
import org.camunda.bpm.engine.identity.User;
import org.camunda.bpm.engine.impl.context.Context;
import org.camunda.bpm.extension.reactor.CamundaReactor;
import org.camunda.bpm.extension.reactor.bus.CamundaSelector;

@CamundaSelector(type = "userTask", event = TaskListener.EVENTNAME_ASSIGNMENT)
public class TaskAssignmentListener implements TaskListener {

  private static final String HOST = "smtp.gmail.com";
  private static final String USER = "camunda.bp@gmail.com";
  private static final String PWD = "";

  private final static Logger LOGGER = Logger.getLogger(TaskAssignmentListener.class.getName());

  //private final Logger logger = LoggerFactory.getLogger(getClass());

  public TaskAssignmentListener() {
    CamundaReactor.eventBus().register(this);
  }

  @Override
  public void notify(DelegateTask delegateTask) {

      //CamundaReactor.eventBus().register(new TaskCreateListener(null));

    String assignee = delegateTask.getAssignee();
    String taskId = delegateTask.getId();

    if (assignee != null) {

      // Get User Profile from User Management
      IdentityService identityService = Context.getProcessEngineConfiguration().getIdentityService();
      User user = identityService.createUserQuery().userId(assignee).singleResult();

      if (user != null) {

        // Get Email Address from User Profile
        String recipient = user.getEmail();

        if (recipient != null && !recipient.isEmpty()) {

          Email email = new SimpleEmail();
          email.setSmtpPort(587);
          email.setSSL(true);
          email.setTLS(true);
          email.setCharset("utf-8");
          email.setHostName(HOST);
          email.setAuthentication(USER, PWD);

          try {
            email.setFrom("noreply@camunda.org");
            email.setSubject("Task assigned: " + delegateTask.getName());
            email.setMsg("Please complete: http://localhost:8080/camunda/app/tasklist/default/#/task/" + taskId);

            email.addTo(recipient);

            email.send();
            LOGGER.info("Task Assignment Email successfully sent to user '" + assignee + "' with address '" + recipient + "'.");            

          } catch (Exception e) {
            LOGGER.log(Level.WARNING, "Could not send email to assignee", e);
          }

        } else {
          LOGGER.warning("Not sending email to user " + assignee + "', user has no email address.");
        }

      } else {
        LOGGER.warning("Not sending email to user " + assignee + "', user is not enrolled with identity service.");
      }

    }

  }

}

但是它不起作用。有什么想法吗?

0 个答案:

没有答案