This repository houses the following (Java) Apache Pulsar Function:
| Function name | Description | API used |
|---|---|---|
JobOpener |
Saves (to database all the details of) a new (instant) job request from a consumer. Then finds (5) online and in-proximity (within 10000 meters radius) workers who can perform the required tasks (marked required by the consumer). Then writes job offer to workers' topics and marks their status as ON-OFFER. (2 seconds) Later monitors the sent offer for (read) receipt, marking OFFLINE the workers who (were sent the offer but) did not respond with a (read) receipt. (After further 9 seconds) checks for acceptance by any of the workers (who received the job offer), marking as ONLINE again the workers who (received the offer but) did not accept. Responds to the consumer accordingly with success (containing job and worker IDs) or with failure (containing reason). | Pulsar Java SDK |
The function has been tested in localrun mode, but can be run in Docker standalone cluster mode too (with minor changes stated in step 3 of LATER RUNS).
- Install Java Runtime Environment & Java Development Kit.
- Install Maven.
- Clone this repository & load it into an IDE (e.g., Eclipse).
- Install Docker Desktop for Windows (with support for Linux Containers, as opposed to Windows Containers).
If you have Windows Home, see Installation for Windows Home. - Install Apache Pulsar as a standalone cluster in Docker:
docker run -it -p 6650:6650 -p 8080:8080 --name pulsar --mount source=pulsardata,target=/pulsar/data --mount source=pulsarconf,target=/pulsar/conf apachepulsar/pulsar:2.5.2 bin/pulsar standalone
Useapachepulsar/pulsar:latestfor the latest version.
NOTE: Since this is being run as an interactive terminal, it will never return for new command(s) to be issued, and will keep providing you with logs. Assuming that the installation is complete when theINFOmessages start intervals. - Install XAMPP.
- Run Apache and MySQL from XAMPP Control Panel.
- Add a user account named
hyperworldly, hostnamelocalhost, and password from this function'sgetNewDatabaseConnectionmethod. Also checkCreate database with same name and grant all privilegeson the page.
- Import the latest version of hyperworldly.sql.
- Run CLI Pulsar consumers, listening to the required topics (e.g., the topics that this function writes to, or the function's input, output and log topics, as specified in the
FunctionConfigundermainmethod):- Open new BASH for the Pulsar (Docker) container:
docker exec -it pulsar /bin/bash - Subscribe to and consume messages for a topic:
bin/pulsar-client consume persistent://public/default/job-opener-input --num-messages 0 --subscription-name JOB-OPENER-INPUT-SUB --subscription-type Exclusive
Logs for the function are printed to the IDE's output on run, so you don't need a consumer for the logs topic.
- Open new BASH for the Pulsar (Docker) container:
- Run the function:
- In localrun mode:
- Implement a
mainmethod with appropriateFunctionConfig
NOTE: Already done. - Run the function in your IDE (e.g., Eclipse).
- Implement a
- In the Docker standalone cluster mode:
- Remove the
mainmethod from the function. - Change
DB_URLtojdbc:mysql://host.docker.internal/hyperworldly?serverTimezone=UTCto access XAMPP's MySQL. - Create a "fat jar" from the cloned repository:
mvn package
Running this command produces 2 JARs (with and without dependencies, accordingly named) in the folder namedtarget. - Copy the
...-jar-with-dependencies.jarfrom thetargetfolder, to the/pulsarfolder in the Docker container namedpulsar. From the IDE's terminal:
docker cp .\target\job-opener-1.0-jar-with-dependencies.jar pulsar:/pulsar - Open BASH for the (Docker) container running Pulsar:
docker exec -it pulsar /bin/bash - Create the function in the Docker container:
bin/pulsar-admin functions create --jar job-opener-1.0-jar-with-dependencies.jar --classname job.opener.sdk.JobOpener --inputs persistent://public/default/job-opener-input --output persistent://public/default/job-opener-output --logTopic persistent://public/default/job-opener-logs --name JO
- Remove the
- In localrun mode:
- Run a CLI Pulsar producer client to send messages to the function's input topic:
- Move the
job-open-request-sample.jsonto the/pulsarfolder inside the Docker container. From the IDE's terminal:
docker cp .\job-open-request-sample.json pulsar:/pulsar - Open new BASH for the Pulsar (Docker) container:
docker exec -it pulsar /bin/bash - Produce the JSON file as message:
bin/pulsar-client produce persistent://public/default/job-opener-input --num-produce 1 --files job-open-request-sample.json
- Move the
- Check the (BASH) consumers, and your IDE's console output for logs!
- Check the schema for the
instant-jobs-opener-inputtopic:
bin/pulsar-admin schemas get job-opener-input
NOTE: This should respond with HTTP 404 Not Found - Copy schema-definition-file.json to Docker container:
docker cp .\schema-definition-file.json pulsar:/pulsar - Upload schema to the input topic:
bin/pulsar-admin schemas upload ob-opener-input --filename schema-definition-file.json
No logs recorded (on the logs topic) in the Docker standalone cluster mode.
Paying no attention as localrun mode is better suited for development, and production cluster will not be Docker standalone cluster.
- Make sure:
- The function is not running.
- There are no consumers listening to the topic.
- From BASH for the Pulsar (Docker) container:
bin/pulsar-admin persistent delete persistent://public/default/job-opener-input
You can use--forceto delete the topic if there are active producers and consumers and you have no alternative way to terminate them.