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));
}
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));
}
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;
}
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;
}
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;
}
Aggregations