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

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

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

package com.example.workflow;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;

@SpringBootApplication
public class Application {

  public static void main(String[] args) throws IOException {

    Runnable serverZyklisch = new ServerZyklisch();
    Runnable serverAzyklisch = new ServerAzyklisch();

    for (int i = 0; i < 2; i++) {
      new Thread(serverZyklisch).start();
      new Thread(serverAzyklisch).start();
    }
    SpringApplication.run(Application.class);
  }
}

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

package com.example.workflow;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ServerAzyklisch implements Runnable, JavaDelegate {
    //new
    int maxClients = 4;
    ExecutorService threadPool = Executors.newFixedThreadPool(maxClients);
    //
    int count = 0;
    private final ServerSocket ssocket;
    static String param;
    HexToByteConverter hexToByteConverter = new HexToByteConverter();
    // 2 TCP Server starten Port 2000, Port 2001
    public ServerAzyklisch(String Pparam) throws IOException {
        ssocket = new ServerSocket(2000);
        param = Pparam;
    }

    public ServerAzyklisch() throws IOException {
        ssocket = new ServerSocket(2000);
    }

    public void run() {
        byte[] buf = new byte[1];
        System.out.println(param+"Paraaam");
        InputStream in;
        OutputStream out = null;
        Socket socket;
        //Thread immer aktiv
        while(true){
            try {
                // Wartet auf Socket Verbindung
                System.out.println("Server is listening on port "+ ssocket.getLocalPort());
                socket = ssocket.accept();
                Socket finalSocket = socket;
                threadPool.submit(() -> {
                    // Communicate with clientSocket, for example:
                    try {
                        Presse p = new Presse(finalSocket);
                        Bohrer b = new Bohrer(finalSocket);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
                count++;
                socket.setSoLinger(true, 1000);
                //Inputstream
                in = socket.getInputStream();
                //Outputstream
                out = socket.getOutputStream();
                //Datenpuffer deklarieren (anlegen)
                byte []data = new byte[132];
            } catch (IOException e) {
                e.printStackTrace();
            }catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void execute(DelegateExecution delegateExecution) throws IOException {
        //String dur = (String) delegateExecution.getVariable("durationglobal");
        //param = dur;

    }
}

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

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Presse implements JavaDelegate {

    String param = "5";
    private HexToByteConverter hexToByteConverter = new HexToByteConverter();
    Socket socket;
    InputStream in;
    OutputStream out;

    byte[]Pressen1hexdump110 = hexToByteConverter.hexStringToByteArray("33333333003d0064000600000004004001c9c78900010000006e0000000000000000000000000001000000000014000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"+param);
    byte[]Pressen2hexdump = hexToByteConverter.hexStringToByteArray("3333333300400065000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
    byte[]Pressen3hexdump = hexToByteConverter.hexStringToByteArray("3333333300400065001400000000004001c9c6e900010000006e000000000000000000000000000100000000001e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");

    public Presse(){

    }

    public Presse(Socket clientSocket) throws IOException {
        this.socket = clientSocket;
        //Inputstream
        in = socket.getInputStream();
        //Outputstream
        out = socket.getOutputStream();
    }

    public void sendMessage() throws IOException {
        byte[] buf = new byte[1];
        in.read(buf);
        while (buf[0] != -1) {
            out.write(Pressen1hexdump110);
            out.write(Pressen2hexdump);
            out.write(Pressen3hexdump);
            //in.read(buf);
        }
    }

    @Override
    public void execute(DelegateExecution delegateExecution) throws Exception {
        param = (String) delegateExecution.getVariable("durationglobal");
        System.out.println("durationglobal:    "+ param);
        //sendMessage();
    }
}
gudnpqoy

gudnpqoy1#

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

相关问题