Customer Service Provider - 6.2

An introduction to Microservices

author
Talend Documentation Team
EnrichVersion
6.4
6.3
6.2
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Real-Time Big Data Platform
task
Administration and Monitoring
Deployment
Design and Development > Designing Routes
EnrichPlatform
Talend Administration Center
Talend CommandLine
Talend ESB
Talend Runtime
Talend Studio
This service will expose a rest interface which will return random customers list as a JSON string.

Procedure

  1. Place the following components as shown in the screenshot below:
    • cBeanRegister - cMQConnectionFactory - cConfig

    • cCXFRS - cSetBody - cWireTap

    • cSEDA - cJavaDSLProcessor - cJMS - cJavaDSLProcessor

  2. Configure the cBeanRegister component to customers as a Bean Id. In the code section, we are going to populate some random data.
    beans.Customers customers = new beans.Customers();
                            beans.Customer customer = new beans.Customer();
                            customer.setFirstName(TalendDataGenerator.getFirstName());
                            customer.setLastName(TalendDataGenerator.getLastName());
                            customer.setCity(TalendDataGenerator.getUsCity());
                            customers.addCustomer(customer);
                            customer = new beans.Customer();
                            customer.setFirstName(TalendDataGenerator.getFirstName());
                            customer.setLastName(TalendDataGenerator.getLastName());
                            customer.setCity(TalendDataGenerator.getUsCity());
                            customers.addCustomer(customer);
                            beanInstance = customers;    

    You have created two Beans namely Customer and Customers.

    The following is the code for the Customers Bean:

    @XmlRootElement
    public class Customers {
                        ArrayList<Customer> customers = new ArrayList<Customer>();
                        @XmlElement
                        public ArrayList<Customer> getCustomers() {
                                            return customers;
                        }
                         public void setCustomers(ArrayList<Customer> customers) {		
                                    this.customers = customers;
                        }
                          public void addCustomer(Customer customer) {
                                    if (this.customers == null) {
                                             customers = new ArrayList<Customer>();
                                    }
                                    this.getCustomers().add(customer);
                                    }
                                    }

    and the following code is for the Customer Bean:

    @XmlRootElement
    @XmlType(propOrder = { "firstName", "lastName", "city" })
    public class Customer {
           private String firstName;
           private String lastName;
           private String city;
    @XmlElement(name = "firstName")
    public String getFirstName() {
           return firstName;
           }
    public void setFirstName(String firstName) {
            this.firstName = firstName;
                            }
    @XmlElement(name = "lastName")
    public String getLastName() {
           return lastName;
    }
    public void setLastName(String lastName) {
            this.lastName = lastName;
                            }
    @XmlElement(name = "city")
    public String getCity() {
            return city;
    }
    public void setCity(String city) {
      this.city = city;
      }
    }
  3. We need to convert the data to XML format. Configure a cConfig component to include the required jar files and instantiate the Jaxb context.
    import org.apache.camel.spi.DataFormat; 
    import org.apache.camel.converter.jaxb.JaxbDataFormat;
    import javax.xml.bind.*;
    
    JAXBContext jc = JAXBContext.newInstance( beans.Customers.class);
    DataFormat jaxb = new JaxbDataFormat(jc);
  4. Configure the cCxFRS component to listen on the endpoint http://127.0.0.1:8041/services/customers. Select the option Produces JSON in Rest API Mapping.
  5. Configure the cSetBody component to call the customers Bean as shown in the screenshot below.
  6. Connect the cSetBody to the cWireTap component to send an audit message to the seda component. Configure the cWireTap with URI seda:toAudit.
  7. Connect the cSEDA component with the cJavaDSLProcessor and configure it with the code .marshal(jaxb) as shown below. Jaxb is the DataFormat handler you already defined in the cConfig component.
  8. Connect with the cJMS and again with the cJavaDSLProcessor with Code .end(). Configure the Queue name audit.
  9. Run the Route and test it with a browser. You should get a customer list in JSON format as shown below.

    Now instead of deploying service to a Talend Runtime server, we will export it as a microservice.

  10. Right click on the route and select Build Route.
  11. In Build type, select ESB Microservices (Spring Boot) and click Finish.
  12. Locate the jar file and run it with a java command.
    Java -jar CustomerProducer***.jar