Skip to main content Skip to complementary content

Customer Service Provider

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

    • cREST - 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 cREST 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. In the Deployment view of the Route tab, select Microservice in the Build Type list. Save the Route.
    Select the Microservice build type.
  11. Right click on the Route and select Build Route.
  12. The Build type is set to Microservices. Click Finish to export the Route.
  13. Locate the jar file and run it with a java command.
    Java -jar CustomerProducer***.jar 

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!