1
votes

I am programming a Spring Boot Application in Connection to Camunda BPMN. Therefore I start my Application like following:

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);
  }
}

I want to start my TCP-Servers in different Threads so that I can accept connection from each different Client. In ServerAzyklisch class I want to accept connection from each client. After that I want to instantiate Objects from Presse, Bohrer, etc.

My ServerAzyklisch class is like:

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;

    }
}

I have following Camunda BPMN:

Camunda BPMN

Now I want to pass these Objects to my Classes Bohrer, Presse,... I want that I am at BPMN at Service Task "Pressen" it executes execute-method in Presse.java. In this execute method I want to do something like p.sendMessage, so that I can send those Message via clientSockets InputStream/Outputstream to the client. Is this possible ? Because if I am Service Task Pressen, which execute the execute method, it want to instantiate a new Presse-Object with no parameters (default constructor). But I want to use the other Object, where clientSocket is passed as parameter.

This is my Presse.java for example:

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();
    }
}
1

1 Answers

0
votes

I am not sure to understand your problem. But if you define Presse object constructor with an argument referencing the other Presse object with the socket, it will works.