Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package router;
- import domain.ConsignmentDetail;
- import com.google.gson.Gson;
- import dao.ConsignmentDAO;
- import domain.CreateOrder;
- import domain.Order;
- import domain.OrderDetail;
- import domain.Sale;
- import java.util.Collection;
- import java.util.List;
- import java.util.Map;
- import javax.swing.JOptionPane;
- import javax.swing.JPasswordField;
- import org.apache.activemq.ActiveMQConnectionFactory;
- import org.apache.camel.CamelContext;
- import org.apache.camel.Exchange;
- import org.apache.camel.Message;
- import org.apache.camel.Processor;
- import org.apache.camel.ProducerTemplate;
- import org.apache.camel.builder.RouteBuilder;
- import org.apache.camel.component.jms.JmsComponent;
- import org.apache.camel.impl.DefaultCamelContext;
- import org.apache.camel.model.dataformat.JsonLibrary;
- /**
- *
- * @author kimhu707
- */
- public class CamelRouteBuilder extends RouteBuilder {
- @Override
- public void configure(){
- //Configuration
- // create default context
- CamelContext camel = new DefaultCamelContext();
- // register ActiveMQ as the JMS handler
- ActiveMQConnectionFactory activeMqFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
- camel.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(activeMqFactory));
- // trust all classes being used to send serialised domain objects
- activeMqFactory.setTrustAllPackages(true);
- // create message producer
- ProducerTemplate producer = camel.createProducerTemplate();
- //Routes
- //Route for Sale update (RMI service)
- from("imaps://outlook.office365.com?username=kimhu707@student.otago.ac.nz"
- + "&password=" + getPassword("Enter you password")
- + "&searchTerm.subject=Vend:SaleUpdate"
- + "&debugMode=false" // set to true if you want to see the authentication details
- + "&folderName=INBOX") // change to whatever folder your Vend messages end up in
- .log("${body}")
- .to("jms:queue:sale");
- from("jms:queue:sale")
- .unmarshal().json(JsonLibrary.Gson, Sale.class) //converting from JSON to java object. Email message is encoded in JSON
- .log("${body.toString()}")
- .to("rmi://localhost:1099/sales?remoteInterfaces=aggregation.ISaleAggregation&method=newSale");
- //Route for Inventory update. Route is only created if count is < reorder point
- from("imaps://outlook.office365.com?username=kimhu707@student.otago.ac.nz"
- + "&password=" + getPassword("Enter you password")
- + "&searchTerm.subject=Vend:InventoryUpdate"
- + "&debugMode=false" // set to true if you want to see the authentication details
- + "&folderName=INBOX") // change to whatever folder your Vend messages end up in
- .convertBodyTo(String.class)
- .log("${body}")
- .to("jms:queue:inventory-update");
- from("jms:queue:inventory-update")
- .unmarshal().json(JsonLibrary.Gson, OrderDetail.class)
- .log("${body.toString()}")
- .choice()
- .when().simple("${body.count} <= ${body.reorder_point}")
- .bean(CreateOrder.class, "createOrder(${body})")
- .to("jms:queue:inventory")
- .otherwise()
- .to("jms:queue:no-inventory");
- from("jms:queue:inventory")
- .removeHeaders("*") // remove headers to stop them being sent to the service
- .setHeader(Exchange.HTTP_METHOD, constant("POST"))
- .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
- .marshal().json(JsonLibrary.Gson)
- .to("http4://localhost:8083/orders");
- //WebSocket for shipping a consignment
- ConsignmentDetail consign = new ConsignmentDetail();
- Gson gson = new Gson();
- String consignJson = gson.toJson(consign);
- from("jms:queue:null") //sending a consignment request to Vend
- .removeHeaders("*") // remove headers to stop them being sent to the service
- .setHeader(Exchange.HTTP_METHOD, constant("POST"))
- .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
- .setHeader("Authorization", simple("Bearer 5j6emC3XZsPEDsjnfKaqO8:iuUnOnRwjJmp6Bx7I"))
- .setBody(constant(consignJson))
- .log("${body}")
- //.marshal().json(JsonLibrary.Gson)
- .to("https://info323.vendhq.com/api/consignment")
- .to("jms:queue:vend-response"); // HTTP response ends up in this queue
- from("jms:queue:vend-response") //Retrieving the consignment ID from Vend response
- .log("Response From the Vend ${body}")
- .setHeader("consignmentId").jsonpath("$.id")
- .setBody().header("consignmentId") //setting body to consignment id
- .log("Vend Consignment Id: ${body}")
- .to("jms:queue:single-order");
- //order from the ajax client via web socket
- from("websocket://localhost:9083/consignments/")
- //.marshal().json(JsonLibrary.Gson)
- .unmarshal().json(JsonLibrary.Gson, List.class)
- .split().body() //split the array of orders
- .log("Received new message via WebSocket: ${body}")
- .to("jms:queue:single-order");
- from("jms:queue:single-order")
- //.bean(OrderDetail.class, "OrderDetail()")
- .log("${body}");
- //.to("jms:queue:products"); // at this point the message is a product object
- }
- public static String getPassword(String prompt) {
- JPasswordField txtPasswd = new JPasswordField();
- int resp = JOptionPane.showConfirmDialog(null, txtPasswd, prompt,
- JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
- if (resp == JOptionPane.OK_OPTION) {
- String password = new String(txtPasswd.getPassword());
- return password;
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement