How to run JBangs apps from the Catalog

This is the second tutorial about JBang. In the first one we covered the basics of this powerful scripting toolkit: Java scripting with JBang . We will now learn how to install Apps from its rich catalog and how to run scripts on the top of Apps.

JBang comes with ability to setup jbang to put scripts/apps into your PATH using jbang app. This is useful to easily make scripts available from anywhere on any Operating System.

As a proof of concept, let’s see how we can run a Kamelet application to produce and consume messages from an Apache Kafka cluster using JBang.

Firstly, make sure that JBang is installed:

curl -Ls https://sh.jbang.dev | bash -s - app setup

Now let’s check the Apps Store: https://www.jbang.dev/appstore/

This is a front-end for all Apps available in the Catalog. To install an App, you can use the following CLI syntax:

jbang app install <app-name>

Therefore, to install the app named “CamelJBang“, we need to run the following command from your terminal:

jbang app install CamelJBang@apache/camel

Next, we will verify that the Apps that are available on your machine:

$ jbang app list
CamelJBang
jbang
quarkus

As you can see, CamelJBang alias has been added to the Apps I have on my machine.

Running the CamelJBangApp

To use our CamelJBangApp, let’s run the helper tool:

$ CamelJBang --help
[jbang] Building jar...
Usage: CamelJBang [-hV] [COMMAND]
A JBang-based Camel app for running Kamelets
  -h, --help      Show this help message and exit.
  -V, --version   Print version information and exit.
Commands:
  run     Run a Kamelet
  search  Search for kameletes, components and patterns (use --help)
  init    Provide init templates for kamelets and bindings

So, there are three commands you can use along with your App: run will launch a Kamelet, search will let you search for Kamelets and Components in the Catalogue. Finally, init will help you to create a minimal configuration for Kamelets.

We will now run the following Kamelets which are available in the example repository:

Here is kafka-unsecured-source.yaml

- route:
    from:
      uri: "kamelet:kafka-not-secured-source"
      parameters:
        brokers: localhost:9092
        topic: test-topic
    steps:
      - log: "${body}"

Here is the kafka-unsecured-sink.yaml

- route:
    from:
      uri: "kamelet:timer-source"
      parameters:
        period: 12399
        message: "Hello Kafka from Camel JBang"
    steps:
      - log: "${body}"
      - to:
          uri: "kamelet:kafka-not-secured-sink"
          parameters:
            brokers: localhost:9092
            topic: test-topic

Before firing our Kamelets, we need to start a Kafka Server. For this purpose, create a docker-compose.yaml file which contains a reference to Zookeeper and Apache Kafka from Strimzi project:

version: '2'

services:

  zookeeper:
    image: strimzi/kafka:0.17.0-kafka-2.4.0
    command: [
      "sh", "-c",
      "bin/zookeeper-server-start.sh config/zookeeper.properties"
    ]
    ports:
      - "2181:2181"
    environment:
      LOG_DIR: /tmp/logs

  kafka:
    image: strimzi/kafka:0.17.0-kafka-2.4.0
    command: [
      "sh", "-c",
      "bin/kafka-server-start.sh config/server.properties --override listeners=$${KAFKA_LISTENERS} --override advertised.listeners=$${KAFKA_ADVERTISED_LISTENERS} --override zookeeper.connect=$${KAFKA_ZOOKEEPER_CONNECT}"
    ]
    depends_on:
      - zookeeper
    ports:
      - "9092:9092"
    environment:
      LOG_DIR: "/tmp/logs"
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
      KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181

Start Apache Kafka as follows:

docker-compose up

We are now ready to start our Kamelets. Here is the Kafka sink:

CamelJBang run kafka-unsecured-sink.yaml 

And then the Source Producer:

CamelJBang run kafka-unsecured-source.yaml

The Camel Producer will connect to Apache Kafka cluster and, using a Timer source, starts sending messages to test-topic:

13:03:04.728 [kafka-producer-network-thread | producer-1] INFO  org.apache.kafka.clients.Metadata - [Producer clientId=producer-1] Cluster ID: dCddr9VrR5CEilSKO6svfw
13:03:05.467 [Camel (CamelJBang) thread #2 - timer://tick] INFO  route2 - Hello Kafka from Camel JBang

The Consumer receives the message from the test-topic:

13:02:56.948 [Camel (CamelJBang) thread #2 - KafkaConsumer[test-topic]] INFO  org.apache.kafka.clients.consumer.internals.SubscriptionState - [Consumer clientId=consumer-567d7268-b0e4-473c-8a48-43c660cf7c7d-2, groupId=567d7268-b0e4-473c-8a48-43c660cf7c7d] Resetting offset for partition test-topic-0 to position FetchPosition{offset=0, offsetEpoch=Optional.empty, currentLeader=LeaderAndEpoch{leader=Optional[localhost:9092 (id: 0 rack: null)], epoch=0}}.
13:03:05.580 [Camel (CamelJBang) thread #2 - KafkaConsumer[test-topic]] INFO  route2 - Hello Kafka from Camel JBang
jbang tutorial

Conclusion

We have just had a brief overview of the JBang universe which already features a large set of apps which can help you to script Java applications even faster.

Found the article helpful? if so please follow us on Socials