如何解决如何将Java对象传递给另一个类以在Camunda的execute方法中执行
我正在编写一个连接Camunda BPMN的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;
}
}
我有以下Camunda BPMN:
现在,我想将这些对象传递给Bohrer,Presse等课程。 我希望我在BPMN的服务任务“ Pressen”中执行Presse.java中的execute-method。在此execute方法中,我想要执行类似p.sendMessage的操作,以便可以通过clientSockets InputStream / Outputstream将这些消息发送到客户端。这可能吗 ? 因为如果我是执行execute方法的服务任务Pressen,则它想实例化没有参数的新Presse-Object(默认构造函数)。但是我想使用另一个对象,其中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();
}
}
解决方法
我不确定您的问题。但是,如果您使用引用套接字的另一个 Presse 对象的参数定义 Presse 对象构造函数,则它将起作用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。