Search in sources :

Example 31 with Consumer

use of org.apache.camel.Consumer in project camel by apache.

the class QuickfixjComponentTest method messagePublication.

@Test
public void messagePublication() throws Exception {
    setUpComponent();
    // Create settings file with both acceptor and initiator
    SessionSettings settings = new SessionSettings();
    settings.setString(Acceptor.SETTING_SOCKET_ACCEPT_PROTOCOL, ProtocolFactory.getTypeString(ProtocolFactory.VM_PIPE));
    settings.setString(Initiator.SETTING_SOCKET_CONNECT_PROTOCOL, ProtocolFactory.getTypeString(ProtocolFactory.VM_PIPE));
    settings.setBool(Session.SETTING_USE_DATA_DICTIONARY, false);
    SessionID acceptorSessionID = new SessionID(FixVersions.BEGINSTRING_FIX44, "ACCEPTOR", "INITIATOR");
    settings.setString(acceptorSessionID, SessionFactory.SETTING_CONNECTION_TYPE, SessionFactory.ACCEPTOR_CONNECTION_TYPE);
    settings.setLong(acceptorSessionID, Acceptor.SETTING_SOCKET_ACCEPT_PORT, 1234);
    setSessionID(settings, acceptorSessionID);
    SessionID initiatorSessionID = new SessionID(FixVersions.BEGINSTRING_FIX44, "INITIATOR", "ACCEPTOR");
    settings.setString(initiatorSessionID, SessionFactory.SETTING_CONNECTION_TYPE, SessionFactory.INITIATOR_CONNECTION_TYPE);
    settings.setLong(initiatorSessionID, Initiator.SETTING_SOCKET_CONNECT_PORT, 1234);
    settings.setLong(initiatorSessionID, Initiator.SETTING_RECONNECT_INTERVAL, 1);
    setSessionID(settings, initiatorSessionID);
    writeSettings(settings, true);
    Endpoint endpoint = component.createEndpoint(getEndpointUri(settingsFile.getName(), null));
    // Start the component and wait for the FIX sessions to be logged on
    final CountDownLatch logonLatch = new CountDownLatch(2);
    final CountDownLatch messageLatch = new CountDownLatch(2);
    Consumer consumer = endpoint.createConsumer(new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            QuickfixjEventCategory eventCategory = (QuickfixjEventCategory) exchange.getIn().getHeader(QuickfixjEndpoint.EVENT_CATEGORY_KEY);
            if (eventCategory == QuickfixjEventCategory.SessionLogon) {
                logonLatch.countDown();
            } else if (eventCategory == QuickfixjEventCategory.AppMessageReceived) {
                messageLatch.countDown();
            }
        }
    });
    ServiceHelper.startService(consumer);
    // will start the component
    camelContext.start();
    assertTrue("Session not created", logonLatch.await(5000, TimeUnit.MILLISECONDS));
    Endpoint producerEndpoint = component.createEndpoint(getEndpointUri(settingsFile.getName(), acceptorSessionID));
    Producer producer = producerEndpoint.createProducer();
    // FIX message to send
    Email email = new Email(new EmailThreadID("ID"), new EmailType(EmailType.NEW), new Subject("Test"));
    Exchange exchange = producer.createExchange(ExchangePattern.InOnly);
    exchange.getIn().setBody(email);
    producer.process(exchange);
    // Produce with no session ID specified, session ID must be in message
    Producer producer2 = endpoint.createProducer();
    email.getHeader().setString(SenderCompID.FIELD, acceptorSessionID.getSenderCompID());
    email.getHeader().setString(TargetCompID.FIELD, acceptorSessionID.getTargetCompID());
    producer2.process(exchange);
    assertTrue("Messages not received", messageLatch.await(5000, TimeUnit.MILLISECONDS));
}
Also used : Processor(org.apache.camel.Processor) Email(quickfix.fix44.Email) EmailType(quickfix.field.EmailType) EmailThreadID(quickfix.field.EmailThreadID) CountDownLatch(java.util.concurrent.CountDownLatch) SessionSettings(quickfix.SessionSettings) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Subject(quickfix.field.Subject) Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Consumer(org.apache.camel.Consumer) Producer(org.apache.camel.Producer) SessionID(quickfix.SessionID) Test(org.junit.Test)

Example 32 with Consumer

use of org.apache.camel.Consumer in project camel by apache.

the class QuickfixjComponentTest method componentStop.

@Test
public void componentStop() throws Exception {
    setUpComponent();
    settings.setString(sessionID, SessionFactory.SETTING_CONNECTION_TYPE, SessionFactory.INITIATOR_CONNECTION_TYPE);
    settings.setLong(sessionID, Initiator.SETTING_SOCKET_CONNECT_PORT, 1234);
    writeSettings();
    Endpoint endpoint = component.createEndpoint(getEndpointUri(settingsFile.getName(), null));
    final CountDownLatch latch = new CountDownLatch(1);
    Consumer consumer = endpoint.createConsumer(new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            QuickfixjEventCategory eventCategory = (QuickfixjEventCategory) exchange.getIn().getHeader(QuickfixjEndpoint.EVENT_CATEGORY_KEY);
            if (eventCategory == QuickfixjEventCategory.SessionCreated) {
                latch.countDown();
            }
        }
    });
    ServiceHelper.startService(consumer);
    // Endpoint automatically starts the consumer
    assertThat(((StatefulService) consumer).isStarted(), is(true));
    // will start the component
    camelContext.start();
    assertTrue("Session not created", latch.await(5000, TimeUnit.MILLISECONDS));
    component.stop();
    assertThat(component.getEngines().get(settingsFile.getName()).isStarted(), is(false));
    // it should still be initialized (ready to start again)
    assertThat(component.getEngines().get(settingsFile.getName()).isInitialized(), is(true));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Endpoint(org.apache.camel.Endpoint) Consumer(org.apache.camel.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Test(org.junit.Test)

Example 33 with Consumer

use of org.apache.camel.Consumer in project camel by apache.

the class SparkComponent method doCreateConsumer.

Consumer doCreateConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters, boolean api) throws Exception {
    String path = basePath;
    if (uriTemplate != null) {
        // make sure to avoid double slashes
        if (uriTemplate.startsWith("/")) {
            path = path + uriTemplate;
        } else {
            path = path + "/" + uriTemplate;
        }
    }
    path = FileUtil.stripLeadingSeparator(path);
    RestConfiguration config = configuration;
    if (config == null) {
        config = camelContext.getRestConfiguration("spark-rest", true);
    }
    Map<String, Object> map = new HashMap<String, Object>();
    if (consumes != null) {
        map.put("accept", consumes);
    }
    // setup endpoint options
    if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
        map.putAll(config.getEndpointProperties());
    }
    if (ObjectHelper.isNotEmpty(path)) {
        // spark-rest uses :name syntax instead of {name} so we need to replace those
        Matcher matcher = PATTERN.matcher(path);
        path = matcher.replaceAll(":$1");
    }
    // prefix path with context-path if configured in rest-dsl configuration
    String contextPath = config.getContextPath();
    if (ObjectHelper.isNotEmpty(contextPath)) {
        contextPath = FileUtil.stripTrailingSeparator(contextPath);
        contextPath = FileUtil.stripLeadingSeparator(contextPath);
        if (ObjectHelper.isNotEmpty(contextPath)) {
            path = contextPath + "/" + path;
        }
    }
    String url;
    if (api) {
        url = "spark-rest:%s:%s?matchOnUriPrefix=true";
    } else {
        url = "spark-rest:%s:%s";
    }
    url = String.format(url, verb, path);
    String query = URISupport.createQueryString(map);
    if (!query.isEmpty()) {
        url = url + "?" + query;
    }
    // get the endpoint
    SparkEndpoint endpoint = camelContext.getEndpoint(url, SparkEndpoint.class);
    setProperties(camelContext, endpoint, parameters);
    // configure consumer properties
    Consumer consumer = endpoint.createConsumer(processor);
    if (config.isEnableCORS()) {
        // if CORS is enabled then configure that on the spark consumer
        if (config.getConsumerProperties() == null) {
            config.setConsumerProperties(new HashMap<String, Object>());
        }
        config.getConsumerProperties().put("enableCors", true);
    }
    if (config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) {
        setProperties(camelContext, consumer, config.getConsumerProperties());
    }
    return consumer;
}
Also used : Consumer(org.apache.camel.Consumer) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) RestConfiguration(org.apache.camel.spi.RestConfiguration)

Example 34 with Consumer

use of org.apache.camel.Consumer in project camel by apache.

the class UndertowComponent method doCreateConsumer.

Consumer doCreateConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters, boolean api) throws Exception {
    String path = basePath;
    if (uriTemplate != null) {
        // make sure to avoid double slashes
        if (uriTemplate.startsWith("/")) {
            path = path + uriTemplate;
        } else {
            path = path + "/" + uriTemplate;
        }
    }
    path = FileUtil.stripLeadingSeparator(path);
    String scheme = "http";
    String host = "";
    int port = 0;
    RestConfiguration config = configuration;
    if (config == null) {
        config = camelContext.getRestConfiguration("undertow", true);
    }
    if (config.getScheme() != null) {
        scheme = config.getScheme();
    }
    if (config.getHost() != null) {
        host = config.getHost();
    }
    int num = config.getPort();
    if (num > 0) {
        port = num;
    }
    // prefix path with context-path if configured in rest-dsl configuration
    String contextPath = config.getContextPath();
    if (ObjectHelper.isNotEmpty(contextPath)) {
        contextPath = FileUtil.stripTrailingSeparator(contextPath);
        contextPath = FileUtil.stripLeadingSeparator(contextPath);
        if (ObjectHelper.isNotEmpty(contextPath)) {
            path = contextPath + "/" + path;
        }
    }
    // if no explicit hostname set then resolve the hostname
    if (ObjectHelper.isEmpty(host)) {
        if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
            host = "0.0.0.0";
        } else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) {
            host = HostUtils.getLocalHostName();
        } else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) {
            host = HostUtils.getLocalIp();
        }
    }
    Map<String, Object> map = new HashMap<String, Object>();
    // build query string, and append any endpoint configuration properties
    if (config.getComponent() == null || config.getComponent().equals("undertow")) {
        // setup endpoint options
        if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
            map.putAll(config.getEndpointProperties());
        }
    }
    boolean cors = config.isEnableCORS();
    if (cors) {
        // allow HTTP Options as we want to handle CORS in rest-dsl
        map.put("optionsEnabled", "true");
    }
    String query = URISupport.createQueryString(map);
    String url;
    if (api) {
        url = "undertow:%s://%s:%s/%s?matchOnUriPrefix=true&httpMethodRestrict=%s";
    } else {
        url = "undertow:%s://%s:%s/%s?httpMethodRestrict=%s";
    }
    // must use upper case for restrict
    String restrict = verb.toUpperCase(Locale.US);
    if (cors) {
        restrict += ",OPTIONS";
    }
    // get the endpoint
    url = String.format(url, scheme, host, port, path, restrict);
    if (!query.isEmpty()) {
        url = url + "&" + query;
    }
    UndertowEndpoint endpoint = camelContext.getEndpoint(url, UndertowEndpoint.class);
    setProperties(camelContext, endpoint, parameters);
    if (!map.containsKey("undertowHttpBinding")) {
        // use the rest binding, if not using a custom http binding
        endpoint.setUndertowHttpBinding(new RestUndertowHttpBinding());
    }
    // configure consumer properties
    Consumer consumer = endpoint.createConsumer(processor);
    if (config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) {
        setProperties(camelContext, consumer, config.getConsumerProperties());
    }
    return consumer;
}
Also used : Consumer(org.apache.camel.Consumer) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RestConfiguration(org.apache.camel.spi.RestConfiguration) Endpoint(org.apache.camel.Endpoint)

Example 35 with Consumer

use of org.apache.camel.Consumer in project camel by apache.

the class MongoDbEndpoint method createConsumer.

public Consumer createConsumer(Processor processor) throws Exception {
    validateConsumerOptions();
    // we never create the collection
    createCollection = false;
    initializeConnection();
    // select right consumer type
    if (consumerType == null) {
        consumerType = MongoDbConsumerType.tailable;
    }
    Consumer consumer;
    if (consumerType == MongoDbConsumerType.tailable) {
        consumer = new MongoDbTailableCursorConsumer(this, processor);
    } else {
        throw new CamelMongoDbException("Consumer type not supported: " + consumerType);
    }
    configureConsumer(consumer);
    return consumer;
}
Also used : Consumer(org.apache.camel.Consumer)

Aggregations

Consumer (org.apache.camel.Consumer)76 Endpoint (org.apache.camel.Endpoint)27 Processor (org.apache.camel.Processor)19 Exchange (org.apache.camel.Exchange)18 Test (org.junit.Test)18 HashMap (java.util.HashMap)10 RestConfiguration (org.apache.camel.spi.RestConfiguration)10 Producer (org.apache.camel.Producer)7 PollingConsumer (org.apache.camel.PollingConsumer)6 File (java.io.File)5 PollingConsumerPollStrategy (org.apache.camel.spi.PollingConsumerPollStrategy)5 FileNotFoundException (java.io.FileNotFoundException)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 SuspendableService (org.apache.camel.SuspendableService)4 ArrayList (java.util.ArrayList)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 CamelContext (org.apache.camel.CamelContext)3 Component (org.apache.camel.Component)3 NoSuchBeanException (org.apache.camel.NoSuchBeanException)3 Route (org.apache.camel.Route)3