Search in sources :

Example 1 with QuickfixjMessageJsonTransformer

use of org.apache.camel.component.quickfixj.examples.transform.QuickfixjMessageJsonTransformer in project camel by apache.

the class RequestReplyExample method run.

public void run() throws Exception {
    final CamelContext context = new DefaultCamelContext();
    final CountDownLatch logonLatch = new CountDownLatch(1);
    final String orderStatusServiceUrl = "http://localhost:9123/order/status";
    RouteBuilder routes = new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            // Synchronize the logon so we don't start sending status requests too early
            from("quickfix:examples/inprocess.cfg?sessionID=FIX.4.2:TRADER->MARKET").filter(header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.SessionLogon)).bean(new CountDownLatchDecrementer("logon", logonLatch));
            // Incoming status requests are passed to the order status service and afterwards we print out that
            // order status being delivered using the json printer.
            from("quickfix:examples/inprocess.cfg?sessionID=FIX.4.2:MARKET->TRADER&exchangePattern=InOut").filter(header(QuickfixjEndpoint.MESSAGE_TYPE_KEY).isEqualTo(MsgType.ORDER_STATUS_REQUEST)).to("log://OrderStatusRequestLog?showAll=true&showOut=true&multiline=true").bean(new MarketOrderStatusService()).bean(new QuickfixjMessageJsonPrinter());
            from("jetty:" + orderStatusServiceUrl).bean(new OrderStatusRequestTransformer()).routingSlip(method(FixSessionRouter.class, "route")).bean(new QuickfixjMessageJsonTransformer(), "transform(${body})");
        }
    };
    context.addRoutes(routes);
    LOG.info("Starting Camel context");
    context.start();
    if (!logonLatch.await(5L, TimeUnit.SECONDS)) {
        throw new IllegalStateException("Logon did not succeed");
    }
    // Send a request to the order status web service.
    // Verify that the response is a JSON response.
    URL orderStatusUrl = new URL(orderStatusServiceUrl + "?sessionID=FIX.4.2:TRADER->MARKET&orderID=abc");
    URLConnection connection = orderStatusUrl.openConnection();
    BufferedReader orderStatusReply = IOHelper.buffered(new InputStreamReader(connection.getInputStream()));
    String line = orderStatusReply.readLine();
    if (!line.equals("\"message\": {")) {
        throw new Exception("Don't appear to be a JSON response");
    } else {
        StringBuilder sb = new StringBuilder();
        while (line != null) {
            sb.append(line);
            sb.append('\n');
            line = orderStatusReply.readLine();
        }
        LOG.info("Web reply:\n" + sb);
    }
    orderStatusReply.close();
    LOG.info("Shutting down Camel context");
    context.stop();
    LOG.info("Example complete");
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) RouteBuilder(org.apache.camel.builder.RouteBuilder) InputStreamReader(java.io.InputStreamReader) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) QuickfixjMessageJsonTransformer(org.apache.camel.component.quickfixj.examples.transform.QuickfixjMessageJsonTransformer) URL(java.net.URL) URLConnection(java.net.URLConnection) CountDownLatchDecrementer(org.apache.camel.component.quickfixj.examples.util.CountDownLatchDecrementer) QuickfixjMessageJsonPrinter(org.apache.camel.component.quickfixj.examples.transform.QuickfixjMessageJsonPrinter) BufferedReader(java.io.BufferedReader)

Aggregations

BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CamelContext (org.apache.camel.CamelContext)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 QuickfixjMessageJsonPrinter (org.apache.camel.component.quickfixj.examples.transform.QuickfixjMessageJsonPrinter)1 QuickfixjMessageJsonTransformer (org.apache.camel.component.quickfixj.examples.transform.QuickfixjMessageJsonTransformer)1 CountDownLatchDecrementer (org.apache.camel.component.quickfixj.examples.util.CountDownLatchDecrementer)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1