SpringBoot—如何将java对象传递给另一个类,以便在带有camunda的execute方法中执行

cfh9epnr  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(503)

我正在编写一个与camundabpmn相关的spring-boot应用程序。因此,我开始我的申请如下:

  1. package com.example.workflow;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import java.io.IOException;
  5. @SpringBootApplication
  6. public class Application {
  7. public static void main(String[] args) throws IOException {
  8. Runnable serverZyklisch = new ServerZyklisch();
  9. Runnable serverAzyklisch = new ServerAzyklisch();
  10. for (int i = 0; i < 2; i++) {
  11. new Thread(serverZyklisch).start();
  12. new Thread(serverAzyklisch).start();
  13. }
  14. SpringApplication.run(Application.class);
  15. }
  16. }

我想在不同的线程中启动我的tcp服务器,这样我就可以接受来自每个不同客户机的连接。在serverazyklisch类中,我希望接受来自每个客户机的连接。之后我想示例化presse、bohrer等的对象。
我的serverazyklisch类如下:

  1. package com.example.workflow;
  2. import org.camunda.bpm.engine.delegate.DelegateExecution;
  3. import org.camunda.bpm.engine.delegate.JavaDelegate;
  4. import java.io.DataInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.net.ServerSocket;
  9. import java.net.Socket;
  10. import java.util.concurrent.ExecutorService;
  11. import java.util.concurrent.Executors;
  12. public class ServerAzyklisch implements Runnable, JavaDelegate {
  13. //new
  14. int maxClients = 4;
  15. ExecutorService threadPool = Executors.newFixedThreadPool(maxClients);
  16. //
  17. int count = 0;
  18. private final ServerSocket ssocket;
  19. static String param;
  20. HexToByteConverter hexToByteConverter = new HexToByteConverter();
  21. // 2 TCP Server starten Port 2000, Port 2001
  22. public ServerAzyklisch(String Pparam) throws IOException {
  23. ssocket = new ServerSocket(2000);
  24. param = Pparam;
  25. }
  26. public ServerAzyklisch() throws IOException {
  27. ssocket = new ServerSocket(2000);
  28. }
  29. public void run() {
  30. byte[] buf = new byte[1];
  31. System.out.println(param+"Paraaam");
  32. InputStream in;
  33. OutputStream out = null;
  34. Socket socket;
  35. //Thread immer aktiv
  36. while(true){
  37. try {
  38. // Wartet auf Socket Verbindung
  39. System.out.println("Server is listening on port "+ ssocket.getLocalPort());
  40. socket = ssocket.accept();
  41. Socket finalSocket = socket;
  42. threadPool.submit(() -> {
  43. // Communicate with clientSocket, for example:
  44. try {
  45. Presse p = new Presse(finalSocket);
  46. Bohrer b = new Bohrer(finalSocket);
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50. });
  51. count++;
  52. socket.setSoLinger(true, 1000);
  53. //Inputstream
  54. in = socket.getInputStream();
  55. //Outputstream
  56. out = socket.getOutputStream();
  57. //Datenpuffer deklarieren (anlegen)
  58. byte []data = new byte[132];
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }catch (Exception e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. }
  66. @Override
  67. public void execute(DelegateExecution delegateExecution) throws IOException {
  68. //String dur = (String) delegateExecution.getVariable("durationglobal");
  69. //param = dur;
  70. }
  71. }

我有以下卡蒙达bpmn:
卡蒙达bpmn
现在我想把这些对象传递给我的类bohrer,presse,。。。我希望我在bpmn的服务任务“pressen”中执行presse.java中的execute方法。在这个execute方法中,我想执行类似于p.sendmessage的操作,这样我就可以通过clientsockets inputstream/outputstream将这些消息发送到客户端。这可能吗?因为如果我是执行execute方法的service task pressen,它希望示例化一个没有参数的新presse对象(默认构造函数)。但是我想使用另一个对象,其中clientsocket作为参数传递。
这是我的presse.java,例如:

  1. import org.camunda.bpm.engine.delegate.DelegateExecution;
  2. import org.camunda.bpm.engine.delegate.JavaDelegate;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.net.Socket;
  7. public class Presse implements JavaDelegate {
  8. String param = "5";
  9. private HexToByteConverter hexToByteConverter = new HexToByteConverter();
  10. Socket socket;
  11. InputStream in;
  12. OutputStream out;
  13. byte[]Pressen1hexdump110 = hexToByteConverter.hexStringToByteArray("33333333003d0064000600000004004001c9c78900010000006e0000000000000000000000000001000000000014000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"+param);
  14. byte[]Pressen2hexdump = hexToByteConverter.hexStringToByteArray("3333333300400065000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
  15. byte[]Pressen3hexdump = hexToByteConverter.hexStringToByteArray("3333333300400065001400000000004001c9c6e900010000006e000000000000000000000000000100000000001e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
  16. public Presse(){
  17. }
  18. public Presse(Socket clientSocket) throws IOException {
  19. this.socket = clientSocket;
  20. //Inputstream
  21. in = socket.getInputStream();
  22. //Outputstream
  23. out = socket.getOutputStream();
  24. }
  25. public void sendMessage() throws IOException {
  26. byte[] buf = new byte[1];
  27. in.read(buf);
  28. while (buf[0] != -1) {
  29. out.write(Pressen1hexdump110);
  30. out.write(Pressen2hexdump);
  31. out.write(Pressen3hexdump);
  32. //in.read(buf);
  33. }
  34. }
  35. @Override
  36. public void execute(DelegateExecution delegateExecution) throws Exception {
  37. param = (String) delegateExecution.getVariable("durationglobal");
  38. System.out.println("durationglobal: "+ param);
  39. //sendMessage();
  40. }
  41. }
gudnpqoy

gudnpqoy1#

我不太明白你的问题。但是,如果使用引用另一个presse对象的参数和套接字定义presse对象构造函数,它将工作。

相关问题