【问题标题】:How to implement a Java Class into a Service Task (Camunda)如何将 Java 类实现为服务任务 (Camunda)
【发布时间】:2022-01-24 19:07:15
【问题描述】:

我正在尝试在我的服务任务中实现 Java 类,但我在 Camunda 中收到以下错误:“无法提交任务表单 943fe63b-7d3f-11ec-9632-70665515d97d:ENGINE-09008 实例化类时出现异常(…) ”。它基本上说它无法加载我的 Java 类。哪里出错了?

    import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;



public class SendEmail {
    final String senderEmail = "random@gmail.com"; //change email address
    final String senderPassword = "random"; //change password
    final String emailSMTPserver = "smtp.gmail.com";
    final String emailServerPort = "465";
    String receiverEmail = null;
    static String emailSubject;
    static String emailBody;
   public SendEmail(String receiverEmail, String subject, String body) {
       //receiver email
       this.receiverEmail = receiverEmail;
       //subject
       this.emailSubject = subject;
       //body
       this.emailBody = body;
       
       Properties props = new Properties();
       props.put("mail.smtp.user",senderEmail);
       props.put("mail.smtp.host", emailSMTPserver);
       props.put("mail.smtp.port", emailServerPort);
       props.put("mail.smtp.starttls.enable", "true");
       props.put("mail.smtp.auth", "true");
       props.put("mail.smtp.socketFactory.port", emailServerPort);
       props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
       props.put("mail.smtp.socketFactory.fallback", "false");
       SecurityManager security = System.getSecurityManager();
       
       try {
           Authenticator auth = new SMTPAuthenticator();
           Session session = Session.getInstance(props, auth);
           MimeMessage msg = new MimeMessage(session);
           msg.setText(emailBody);
           //System.out.println(emailBody);
           msg.setSubject(emailSubject);
           msg.setFrom(new InternetAddress(senderEmail));
           msg.addRecipient(Message.RecipientType.TO, new InternetAddress(receiverEmail) );
           Transport.send(msg);
           System.out.println("Message sent!");
           String className = this.getClass().getSimpleName();
           System.out.println(className);
           
           
       }
       
       catch (Exception e) {
           e.printStackTrace();
       }
               
       
   }
    
    public class SMTPAuthenticator extends javax.mail.Authenticator{
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(senderEmail, senderPassword );
        }
    }
        
    public class Tester {
public static void main( String [] args) {
    SendEmail send = new SendEmail("testemail@gmail.com", "Test", "Camunda" ); //change receiver email
}
    }

这里是服务任务中定义的打印:

【问题讨论】:

  • 为什么你没有accepted 任何你过去问题的答案?在提出新问题之前,您应该这样做。

标签: java camunda camunda-modeler


【解决方案1】:

这不是它的工作方式。请遵循 Camunda Docs 示例https://docs.camunda.org/get-started/java-process-app/service-task/

你的类需要实现接口org.camunda.bpm.engine.delegate.JavaDelegate,看这个例子https://docs.camunda.org/get-started/java-process-app/service-task/#add-a-javadelegate-implementation

package org.camunda.bpm.getstarted.loanapproval;

import java.util.logging.Logger;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;

public class ProcessRequestDelegate implements JavaDelegate {

  private final static Logger LOGGER = Logger.getLogger("LOAN-REQUESTS");

  public void execute(DelegateExecution execution) throws Exception {
    LOGGER.info("Processing request by '" + execution.getVariable("customerId") + "'...");
  }

}

此外,您可以在文档中阅读:

您需要在 Java 类属性字段中提供类的完全限定类名。在我们的例子中,这是 org.camunda.bpm.getstarted.loanapproval.ProcessRequestDelegate。

您需要在流程模型中指定完全限定名称。此外,您的代理需要可由引擎访问,例如通过应用程序服务器中的共享引擎。

我建议您还可以查看有关从流程https://camunda.com/best-practices/invoking-services-from-the-process/调用服务的最佳实践

【讨论】:

    【解决方案2】:

    Zelldon 的回答是正确的。另外,我鼓励你不要使用不灵活的实现类型 Java 类,而是要么

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-19
      • 1970-01-01
      • 2022-12-21
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 2018-08-18
      相关资源
      最近更新 更多