Search in sources :

Example 1 with CountDownLatchDecrementer

use of org.apache.camel.component.quickfixj.examples.util.CountDownLatchDecrementer 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)

Example 2 with CountDownLatchDecrementer

use of org.apache.camel.component.quickfixj.examples.util.CountDownLatchDecrementer in project camel by apache.

the class SimpleMessagingExample method sendMessage.

public void sendMessage() throws Exception {
    DefaultCamelContext context = new DefaultCamelContext();
    final CountDownLatch logonLatch = new CountDownLatch(2);
    final CountDownLatch receivedMessageLatch = new CountDownLatch(1);
    RouteBuilder routes = new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            // Release latch when session logon events are received
            // We expect two events, one for the trader session and one for the market session
            from("quickfix:examples/inprocess.cfg").filter(header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.SessionLogon)).bean(new CountDownLatchDecrementer("logon", logonLatch));
            // For all received messages, print the JSON-formatted message to stdout
            from("quickfix:examples/inprocess.cfg").filter(PredicateBuilder.or(header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.AdminMessageReceived), header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.AppMessageReceived))).bean(new QuickfixjMessageJsonPrinter());
            // If the market session receives an email then release the latch
            from("quickfix:examples/inprocess.cfg?sessionID=FIX.4.2:MARKET->TRADER").filter(header(QuickfixjEndpoint.MESSAGE_TYPE_KEY).isEqualTo(MsgType.EMAIL)).bean(new CountDownLatchDecrementer("message", receivedMessageLatch));
        }
    };
    context.addRoutes(routes);
    LOG.info("Starting Camel context");
    context.start();
    if (!logonLatch.await(5L, TimeUnit.SECONDS)) {
        throw new IllegalStateException("Logon did not succeed");
    }
    String marketUri = "quickfix:examples/inprocess.cfg?sessionID=FIX.4.2:TRADER->MARKET";
    Producer producer = context.getEndpoint(marketUri).createProducer();
    Email email = TestSupport.createEmailMessage("Example");
    Exchange exchange = producer.createExchange(ExchangePattern.InOnly);
    exchange.getIn().setBody(email);
    producer.process(exchange);
    if (!receivedMessageLatch.await(5L, TimeUnit.SECONDS)) {
        throw new IllegalStateException("Message did not reach market");
    }
    LOG.info("Message received, shutting down Camel context");
    context.stop();
    LOG.info("Example complete");
}
Also used : QuickfixjMessageJsonPrinter(org.apache.camel.component.quickfixj.examples.transform.QuickfixjMessageJsonPrinter) Exchange(org.apache.camel.Exchange) Email(quickfix.fix42.Email) RouteBuilder(org.apache.camel.builder.RouteBuilder) Producer(org.apache.camel.Producer) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CountDownLatchDecrementer(org.apache.camel.component.quickfixj.examples.util.CountDownLatchDecrementer)

Example 3 with CountDownLatchDecrementer

use of org.apache.camel.component.quickfixj.examples.util.CountDownLatchDecrementer in project camel by apache.

the class AuthenticationExample method run.

public void run() throws Exception {
    DefaultCamelContext context = new DefaultCamelContext();
    final CountDownLatch logoutLatch = new CountDownLatch(1);
    RouteBuilder routes = new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            // Modify the outgoing logon message to add a password
            // The modified message will be sent from the FIX engine when the message exchange completes
            from("quickfix:examples/inprocess.cfg?sessionID=FIX.4.2:TRADER->MARKET").filter(PredicateBuilder.and(header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.AdminMessageSent), header(QuickfixjEndpoint.MESSAGE_TYPE_KEY).isEqualTo(MsgType.LOGON))).bean(new CredentialInjector("PASSWORD"));
            // Release latch when the trader received a logout message
            from("quickfix:examples/inprocess.cfg?sessionID=FIX.4.2:TRADER->MARKET").filter(header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.SessionLogoff)).bean(new CountDownLatchDecrementer("logout", logoutLatch));
            // Reject all logons on market side
            // Demonstrates how to validate logons
            from("quickfix:examples/inprocess.cfg?sessionID=FIX.4.2:MARKET->TRADER").filter(PredicateBuilder.and(header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.AdminMessageReceived), header(QuickfixjEndpoint.MESSAGE_TYPE_KEY).isEqualTo(MsgType.LOGON))).bean(new LogonAuthenticator());
        }
    };
    context.addRoutes(routes);
    LOG.info("Starting Camel context");
    context.start();
    if (!logoutLatch.await(5L, TimeUnit.SECONDS)) {
        throw new IllegalStateException("Logout was not received");
    }
    context.stop();
    LOG.info("Example complete");
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CountDownLatchDecrementer(org.apache.camel.component.quickfixj.examples.util.CountDownLatchDecrementer)

Example 4 with CountDownLatchDecrementer

use of org.apache.camel.component.quickfixj.examples.util.CountDownLatchDecrementer in project camel by apache.

the class DynamicRoutingExample method sendMessage.

public void sendMessage() throws Exception {
    DefaultCamelContext context = new DefaultCamelContext();
    final CountDownLatch logonLatch = new CountDownLatch(4);
    final CountDownLatch receivedMessageLatch = new CountDownLatch(1);
    RouteBuilder routes = new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            // Release latch when session logon events are received
            // We expect four logon events (four sessions)
            from("quickfix:examples/gateway.cfg").filter(header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.SessionLogon)).bean(new CountDownLatchDecrementer("logon", logonLatch));
            // Dynamic router -- Uses FIX DeliverTo tags
            from("quickfix:examples/gateway.cfg").filter(header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.AppMessageReceived)).recipientList(method(new FixMessageRouter("quickfix:examples/gateway.cfg")));
            // Logger app messages as JSON
            from("quickfix:examples/gateway.cfg").filter(PredicateBuilder.or(header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.AppMessageReceived), header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.AppMessageSent))).bean(new QuickfixjMessageJsonPrinter());
            // If the trader@2 session receives an email then release the latch
            from("quickfix:examples/gateway.cfg?sessionID=FIX.4.2:TRADER@2->GATEWAY").filter(PredicateBuilder.and(header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.AppMessageReceived), header(QuickfixjEndpoint.MESSAGE_TYPE_KEY).isEqualTo(MsgType.EMAIL))).bean(new CountDownLatchDecrementer("message", receivedMessageLatch));
        }
    };
    context.addRoutes(routes);
    LOG.info("Starting Camel context");
    context.start();
    // synchronization due to app messages being sent before being logged on
    if (!logonLatch.await(5, TimeUnit.SECONDS)) {
        throw new IllegalStateException("Logon did not complete");
    }
    String gatewayUri = "quickfix:examples/gateway.cfg?sessionID=FIX.4.2:TRADER@1->GATEWAY";
    Endpoint gatewayEndpoint = context.getEndpoint(gatewayUri);
    Producer producer = gatewayEndpoint.createProducer();
    Email email = TestSupport.createEmailMessage("Dynamic Routing Example");
    email.getHeader().setString(DeliverToCompID.FIELD, "TRADER@2");
    LOG.info("Sending routed message");
    Exchange exchange = producer.createExchange(ExchangePattern.InOnly);
    exchange.getIn().setBody(email);
    producer.process(exchange);
    if (!receivedMessageLatch.await(5, TimeUnit.SECONDS)) {
        throw new IllegalStateException("Message did not reach target");
    }
    LOG.info("Message received, shutting down Camel context");
    context.stop();
    LOG.info("Dynamic routing example complete");
}
Also used : QuickfixjMessageJsonPrinter(org.apache.camel.component.quickfixj.examples.transform.QuickfixjMessageJsonPrinter) Exchange(org.apache.camel.Exchange) Email(quickfix.fix42.Email) RouteBuilder(org.apache.camel.builder.RouteBuilder) Endpoint(org.apache.camel.Endpoint) QuickfixjEndpoint(org.apache.camel.component.quickfixj.QuickfixjEndpoint) Producer(org.apache.camel.Producer) FixMessageRouter(org.apache.camel.component.quickfixj.examples.routing.FixMessageRouter) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CountDownLatchDecrementer(org.apache.camel.component.quickfixj.examples.util.CountDownLatchDecrementer)

Example 5 with CountDownLatchDecrementer

use of org.apache.camel.component.quickfixj.examples.util.CountDownLatchDecrementer in project camel by apache.

the class TradeExecutorExample method sendMessage.

public void sendMessage() throws Exception {
    DefaultCamelContext context = new DefaultCamelContext();
    context.addComponent("trade-executor", new TradeExecutorComponent());
    final CountDownLatch logonLatch = new CountDownLatch(2);
    final CountDownLatch executionReportLatch = new CountDownLatch(2);
    RouteBuilder routes = new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            // Release latch when session logon events are received
            from("quickfix:examples/inprocess.cfg").filter(header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.SessionLogon)).bean(new CountDownLatchDecrementer("logon", logonLatch));
            from("quickfix:examples/inprocess.cfg?sessionID=FIX.4.2:MARKET->TRADER").filter(header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.AppMessageReceived)).to("trade-executor:market");
            from("trade-executor:market").to("quickfix:examples/inprocess.cfg");
            // Logger app messages as JSON
            from("quickfix:examples/inprocess.cfg").filter(PredicateBuilder.or(header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.AppMessageReceived), header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.AppMessageSent))).bean(new QuickfixjMessageJsonPrinter());
            // Release latch when trader receives execution report
            from("quickfix:examples/inprocess.cfg?sessionID=FIX.4.2:TRADER->MARKET").filter(PredicateBuilder.and(header(QuickfixjEndpoint.EVENT_CATEGORY_KEY).isEqualTo(QuickfixjEventCategory.AppMessageReceived), header(QuickfixjEndpoint.MESSAGE_TYPE_KEY).isEqualTo(MsgType.EXECUTION_REPORT))).bean(new CountDownLatchDecrementer("execution report", executionReportLatch));
        }
    };
    context.addRoutes(routes);
    LOG.info("Starting Camel context");
    context.start();
    // synchronization due to app messages being sent before being logged on
    if (!logonLatch.await(5, TimeUnit.SECONDS)) {
        throw new IllegalStateException("Logon did not complete");
    }
    String gatewayUri = "quickfix:examples/inprocess.cfg?sessionID=FIX.4.2:TRADER->MARKET";
    Endpoint gatewayEndpoint = context.getEndpoint(gatewayUri);
    Producer producer = gatewayEndpoint.createProducer();
    LOG.info("Sending order");
    NewOrderSingle order = createNewOrderMessage();
    Exchange exchange = producer.createExchange(ExchangePattern.InOnly);
    exchange.getIn().setBody(order);
    producer.process(exchange);
    if (!executionReportLatch.await(5, TimeUnit.SECONDS)) {
        throw new IllegalStateException("Did not receive execution reports");
    }
    LOG.info("Message received, shutting down Camel context");
    context.stop();
    LOG.info("Order execution example complete");
}
Also used : QuickfixjMessageJsonPrinter(org.apache.camel.component.quickfixj.examples.transform.QuickfixjMessageJsonPrinter) NewOrderSingle(quickfix.fix42.NewOrderSingle) Exchange(org.apache.camel.Exchange) RouteBuilder(org.apache.camel.builder.RouteBuilder) Endpoint(org.apache.camel.Endpoint) QuickfixjEndpoint(org.apache.camel.component.quickfixj.QuickfixjEndpoint) Producer(org.apache.camel.Producer) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CountDownLatchDecrementer(org.apache.camel.component.quickfixj.examples.util.CountDownLatchDecrementer)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)5 RouteBuilder (org.apache.camel.builder.RouteBuilder)5 CountDownLatchDecrementer (org.apache.camel.component.quickfixj.examples.util.CountDownLatchDecrementer)5 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)5 QuickfixjMessageJsonPrinter (org.apache.camel.component.quickfixj.examples.transform.QuickfixjMessageJsonPrinter)4 Exchange (org.apache.camel.Exchange)3 Producer (org.apache.camel.Producer)3 Endpoint (org.apache.camel.Endpoint)2 QuickfixjEndpoint (org.apache.camel.component.quickfixj.QuickfixjEndpoint)2 Email (quickfix.fix42.Email)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 CamelContext (org.apache.camel.CamelContext)1 FixMessageRouter (org.apache.camel.component.quickfixj.examples.routing.FixMessageRouter)1 QuickfixjMessageJsonTransformer (org.apache.camel.component.quickfixj.examples.transform.QuickfixjMessageJsonTransformer)1 NewOrderSingle (quickfix.fix42.NewOrderSingle)1