Showing posts with label saga. Show all posts
Showing posts with label saga. Show all posts

Tuesday, November 16, 2021

Camel Saga & Narayana LRA


 

Ref: 

  • https://jbossts.blogspot.com/2017/12/narayana-lra-implementation-of-saga.html
  • https://camel.apache.org/components/3.7.x/eips/saga-eip.html
  • https://github.com/nicolaferraro/camel-saga-quickstart

Use Redhat Fuse to create Integration Project for editing camel file with visual editor

The Pom file look like

<?xml version="1.0" encoding="UTF-8"?>

<project

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"

xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<modelVersion>4.0.0</modelVersion>

<groupId>org.mycompany</groupId>

<artifactId>camel-ose-springboot-xml</artifactId>

<version>1.0.0-SNAPSHOT</version>

<name>Fabric8 :: Quickstarts :: Spring-Boot :: Camel XML</name>

<description>Spring Boot example running a Camel route defined in XML</description>

<properties>

<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>

<fuse.version>7.7.0.fuse-sb2-770010-redhat-00001</fuse.version>

</properties>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.jboss.redhat-fuse</groupId>

<artifactId>fuse-springboot-bom</artifactId>

<version>7.7.0.fuse-sb2-770010-redhat-00001</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-undertow</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

<exclusions>

<exclusion>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>org.apache.camel</groupId>

<artifactId>camel-spring-boot-starter</artifactId>


</dependency>

<dependency>

<groupId>org.apache.camel</groupId>

<artifactId>camel-lra-starter</artifactId>

<version>2.21.0</version>

</dependency>

<dependency>

<groupId>org.apache.camel</groupId>

<artifactId>camel-servlet-starter</artifactId>

</dependency>

<dependency>

<groupId>org.apache.camel</groupId>

<artifactId>camel-http4-starter</artifactId>

</dependency>

</dependencies>

<repositories>

<repository>

<id>red-hat-ga-repository</id>

<url>https://maven.repository.redhat.com/ga</url>

</repository>

</repositories>

<pluginRepositories>

<pluginRepository>

<id>red-hat-ga-repository</id>

<url>https://maven.repository.redhat.com/ga</url>

</pluginRepository>

</pluginRepositories>

<build>

<defaultGoal>spring-boot:run</defaultGoal>

<plugins>

<plugin>

<artifactId>maven-compiler-plugin</artifactId>

<version>${maven-compiler-plugin.version}</version>

<configuration>

<source>1.8</source>

<target>1.8</target>

</configuration>

</plugin>

<plugin>

<groupId>org.jboss.redhat-fuse</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<version>${fuse.version}</version>

<executions>

<execution>

<goals>

<goal>repackage</goal>

</goals>

</execution>

</executions>

</plugin>

<plugin>

<groupId>org.jboss.redhat-fuse</groupId>

<artifactId>fabric8-maven-plugin</artifactId>

<version>${fuse.version}</version>

<executions>

<execution>

<goals>

<goal>resource</goal>

<goal>build</goal>

</goals>

</execution>

</executions>

</plugin>

</plugins>

</build>

<profiles>

<profile>

<id>jdk9+-build</id>

<activation>

<jdk>[9,)</jdk>

</activation>

<dependencies>

<dependency>

<groupId>javax.annotation</groupId>

<artifactId>javax.annotation-api</artifactId>

</dependency>

<dependency>

<groupId>javax.xml.ws</groupId>

<artifactId>jaxws-api</artifactId>

<exclusions>

<exclusion>

<artifactId>jaxb-api</artifactId>

<groupId>javax.xml.bind</groupId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>jakarta.xml.bind</groupId>

<artifactId>jakarta.xml.bind-api</artifactId>

<version>2.3.3</version>

</dependency>

<dependency>

<groupId>com.sun.activation</groupId>

<artifactId>javax.activation</artifactId>

</dependency>

<dependency>

<groupId>org.apache.geronimo.specs</groupId>

<artifactId>geronimo-ws-metadata_2.0_spec</artifactId>

<version>1.1.3</version>

</dependency>

<dependency>

<groupId>com.sun.xml.messaging.saaj</groupId>

<artifactId>saaj-impl</artifactId>

</dependency>

<dependency>

<groupId>org.apache.geronimo.specs</groupId>

<artifactId>geronimo-jta_1.1_spec</artifactId>

</dependency>

<dependency>

<groupId>org.jboss.spec.javax.rmi</groupId>

<artifactId>jboss-rmi-api_1.0_spec</artifactId>

<version>1.0.6.Final</version>

</dependency>

</dependencies>

</profile>

</profiles>

</project>

File application.yaml

camel:

  springboot:

    name: MyCamel

    main-run-controller: true

  service:

    lra:

      enabled: true

      coordinator-url: http://localhost:8081

      local-participant-url: http://localhost:8080/api

  component:

    servlet:

      mapping:

        context-path: /api/*


logging:

  config: classpath:logback.xml


server:

  address: 0.0.0.0


management:

  address: 0.0.0.0

  port: 8080

endpoints:

  enabled: false

  health:

    enabled: true



#spring:

#  main:

#    web-application-type: none


Ok, Now create Saga in Camel 

<route id="simple-route">
            <from id="route-timer" uri="timer://foo?period=10000"/>
            <saga id="_saga1">
                <to id="_to1" uri="direct:order"/>
                <to id="_to2" uri="direct:credit"/>
            </saga>
            <log id="route-log" message=">>> ${body}"/>
        </route>
        <route id="_route1">
            <from id="_from1" uri="direct:order"/>
            <saga id="_saga2">
                <transform id="_transform1">
                    <header>Exchange.SAGA_LONG_RUNNING_ACTION</header>
                </transform>
                <log id="_log1" message="order completed with body ${body}"/>
                <compensation uri="direct:cancel"/>
                <completion uri="direct:refund"/>
            </saga>
        </route>
        <route id="_route2">
            <from id="_from2" uri="direct:credit"/>
            <saga id="_saga3" propagation="MANDATORY">
                <transform id="_transform2">
                    <header>Exchange.SAGA_LONG_RUNNING_ACTION</header>
                </transform>
                <log id="_log2" message="credit completed with body ${body}"/>
                <compensation uri="direct:refund"/>
                <completion uri="direct:cancel"/>
            </saga>
        </route>
        <route id="_route3">
            <from id="_from3" uri="direct:refund"/>
            <log id="_log3" message="refund"/>
        </route>
        <route id="_route4">
            <from id="_from4" uri="direct:cancel"/>
            <log id="_log4" message="credit completed"/>
        </route>

We have to run LRA coordinator with docker:
docker run -d -p 8081:8080 -e JAVA_ARGS=-Djava.net.preferIPv4Stack=true docker.io/jbosstm/lra-coordinator:5.8.2.Final

Now,We can run appliction

Install and use xorg-server on macOS via Homebrew

  The instructions to install and use xorg-server on macOS via Homebrew: Install Homebrew (if you haven't already): /bin/bash -c ...