Skip to content

Instantly share code, notes, and snippets.

@hodrigohamalho
Last active April 15, 2020 13:00
Show Gist options
  • Save hodrigohamalho/da949a2fe65624335fc685311689a163 to your computer and use it in GitHub Desktop.
Save hodrigohamalho/da949a2fe65624335fc685311689a163 to your computer and use it in GitHub Desktop.
Fuse demos

Integration Demos

Integrating message-oriented middleware with a RESTful API using AMQ Online

This is available as a walkthrough. On Integreatly environment.

1) Create a queue

work-queue-requests

2) Show the applications working

https://order-entry-ui-admin-tutorial-f6e1.apps.latam-6090.open.redhat.com
https://order-management-ui-admin-tutorial-f6e1.apps.latam-6090.open.redhat.com

2) Open API Specification

https://orders-fuse-api-admin-tutorial-f6e1.apps.latam-6090.open.redhat.com/openapi.json

3) Setup Broker AMQP

url: amqp://messaging-9dvrzw19hw.enmasse.svc:5672?amqp.saslMechanisms=PLAIN
user: user-05ed69f4-7ed8-11ea-b8df-0a580a010007
passwd: I4bvMTyNjwfP0SWkcDQq2bUrUYVyoc

4) Integration

work-queue-requests
{	"$schema": "http://json-schema.org/draft-04/schema#",	"type": "object",	"properties": {		"product": {			"type": "string"		},		"quantity": {			"type": "number"		},		"datetime": {			"type": "string"		},		"message_id": {			"type": "string"		}	}}

API First approach

Prepare the environment in Openshift

oc new-project fuse-demo
oc new-app --template=postgresql-persistent --param=POSTGRESQL_PASSWORD=redhat --param=POSTGRESQL_USER=redhat --param=POSTGRESQL_DATABASE=sampledb
Create and populate database
psql -U redhat -d sampledb
CREATE TABLE users(
 id serial PRIMARY KEY,
 name VARCHAR (50),
 phone VARCHAR (50),
 age integer
);
INSERT INTO users(name, phone, age) VALUES  ('Rodrigo Ramalho', '(11) 95474-8099', 30);
INSERT INTO users(name, phone, age) VALUES  ('Rafael Ramalho', '(61) 99988-8029', 32);
INSERT INTO users(name, phone, age) VALUES  ('Thiago Araki', '(11) 9999-9999', 33);
INSERT INTO users(name, phone, age) VALUES  ('Gustavo Luszynsk', '(11) 9999-9999', 33);
INSERT INTO users(name, phone, age) VALUES  ('Rafael Tuelho', '(11) 9999-9999', 33);

Configure in Red Hat Fuse Online

Configure a database connector in Fuse Online

url: jdbc:postgresql://postgresql.fuse-demo:5432/sampledb
user: redhat
password: redhat

During the API definition, use this schema in datatype:

{
    "id": 0,
    "name": "Rodrigo Ramalho",
    "phone": "11 95474-8099",
    "age": 30
}

During the flow implementation, use this queries:

Get All

SELECT * FROM USERS

CREATE USER

INSERT INTO USERS(NAME,PHONE,AGE) VALUES(:#NAME,:#PHONE,:#AGE);

Camel K

Pre requirements

Create a project

oc new-project livedemo

Install Kamel operator

kamel install

Create a CamelBot.java class and run just to validate if everything is working fine

import org.apache.camel.builder.RouteBuilder;
public class CamelBot extends RouteBuilder {
  @Override
  public void configure() throws Exception {
    from("timer:demo")
      .log("working :)")
  }
}
kamel run CamelBot.java --dev

Start to work in the Telegram Bot, the final version will be something like it:

CamelBot.java

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.Exchange;
import org.apache.camel.model.dataformat.JsonLibrary;
public class CamelBot extends RouteBuilder {
  @Override
  public void configure() throws Exception {
    from("telegram:bots/{{token}}")
      .log("command received ${body}")
      .convertBodyTo(String.class)
      .choice()
        .when(simple("${body} == 'joke'"))
          .log("action joke triggered")
          .to("http4://api.icndb.com/jokes/random")
          .unmarshal().json(JsonLibrary.Jackson)
          .transform(simple("${body[value][joke]}"))
          .to("telegram:bots/{{token}}")
          .to("slack:#integration")
        .when(simple("${body} == 'publish'"))
          .log("action publish triggered")
        .otherwise()
          .setBody().simple("Action not found. Supported actions:\n *joke\n*publish")
          .to("telegram:bots/{{token}}")
          .to("slack:#integration");
  }
}
kamel run CamelBot.java --dev -p token=1283x

To run Slack Integration

Create an application.properties file:

camel.component.slack.webhook-url=https://hooks.slack.com/services/your-token

Create a secret

oc create secret generic camelk-bot --from-file application.properties

Run pointing application.properties

kamel run CamelBot.java --dev --secret=camelk-bot -p token=telegram-token-here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment