use of org.apache.camel.Consumer in project camel by apache.
the class CronScheduledRoutePolicyTest method testScheduledSuspendRoutePolicy.
@Test
public void testScheduledSuspendRoutePolicy() throws Exception {
context.getComponent("quartz", QuartzComponent.class).setPropertiesFile("org/apache/camel/routepolicy/quartz/myquartz.properties");
context.addRoutes(new RouteBuilder() {
public void configure() {
CronScheduledRoutePolicy policy = new CronScheduledRoutePolicy();
policy.setRouteSuspendTime("*/3 * * * * ?");
from("direct:start").routeId("test").routePolicy(policy).to("mock:unreachable");
}
});
context.start();
Thread.sleep(5000);
// when suspending its only the consumer that suspends
// there is a ticket to improve this
Consumer consumer = context.getRoute("test").getConsumer();
SuspendableService ss = (SuspendableService) consumer;
assertTrue("Consumer should be suspended", ss.isSuspended());
}
use of org.apache.camel.Consumer in project camel by apache.
the class QuartzScheduledPollConsumerJob method execute.
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
LOG.trace("Execute job: {}", context);
CamelContext camelContext = getCamelContext(context);
Runnable task = (Runnable) context.getJobDetail().getJobDataMap().get("task");
if (task == null) {
// if not task then use the route id to lookup the consumer to be used as the task
String routeId = (String) context.getJobDetail().getJobDataMap().get("routeId");
if (routeId != null && camelContext != null) {
// find the consumer
for (Route route : camelContext.getRoutes()) {
if (route.getId().equals(routeId)) {
Consumer consumer = route.getConsumer();
if (consumer instanceof Runnable) {
task = (Runnable) consumer;
break;
}
}
}
}
}
if (task != null) {
LOG.trace("Running task: {}", task);
task.run();
}
}
use of org.apache.camel.Consumer in project camel by apache.
the class CronScheduledRoutePolicyTest method testScheduledSuspendRoutePolicy.
@Test
public void testScheduledSuspendRoutePolicy() throws Exception {
context.getComponent("quartz2", QuartzComponent.class).setPropertiesFile("org/apache/camel/routepolicy/quartz2/myquartz.properties");
context.addRoutes(new RouteBuilder() {
public void configure() {
CronScheduledRoutePolicy policy = new CronScheduledRoutePolicy();
policy.setRouteSuspendTime("*/3 * * * * ?");
from("direct:start").routeId("test").routePolicy(policy).to("mock:unreachable");
}
});
context.start();
Thread.sleep(5000);
// when suspending its only the consumer that suspends
// there is a ticket to improve this
Consumer consumer = context.getRoute("test").getConsumer();
SuspendableService ss = (SuspendableService) consumer;
assertTrue("Consumer should be suspended", ss.isSuspended());
}
use of org.apache.camel.Consumer in project camel by apache.
the class RestletComponent method createConsumer.
@Override
public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters) 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 = "";
// use the component's port as the default value
int port = this.getPort();
// if no explicit port/host configured, then use port from rest configuration
RestConfiguration config = configuration;
if (config == null) {
config = camelContext.getRestConfiguration("restlet", 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("restlet")) {
// setup endpoint options
if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
map.putAll(config.getEndpointProperties());
}
}
// allow HTTP Options as we want to handle CORS in rest-dsl
boolean cors = config.isEnableCORS();
String query = URISupport.createQueryString(map);
String url;
// must use upper case for restrict
String restrict = verb.toUpperCase(Locale.US);
if (cors) {
restrict += ",OPTIONS";
}
if (port > 0) {
url = "restlet:%s://%s:%s/%s?restletMethods=%s";
url = String.format(url, scheme, host, port, path, restrict);
} else {
// It could use the restlet servlet transport
url = "restlet:/%s?restletMethods=%s";
url = String.format(url, path, restrict);
}
if (!query.isEmpty()) {
url = url + "&" + query;
}
// get the endpoint
RestletEndpoint endpoint = camelContext.getEndpoint(url, RestletEndpoint.class);
setProperties(camelContext, endpoint, parameters);
// the endpoint must be started before creating the consumer
ServiceHelper.startService(endpoint);
// 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 JettyHttpComponent 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;
// if no explicit port/host configured, then use port from rest configuration
RestConfiguration config = configuration;
if (config == null) {
config = camelContext.getRestConfiguration("jetty", 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("jetty")) {
// 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 = "jetty:%s://%s:%s/%s?matchOnUriPrefix=true&httpMethodRestrict=%s";
} else {
url = "jetty:%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;
}
JettyHttpEndpoint endpoint = camelContext.getEndpoint(url, JettyHttpEndpoint.class);
setProperties(camelContext, endpoint, parameters);
if (!map.containsKey("httpBindingRef")) {
// use the rest binding, if not using a custom http binding
endpoint.setHttpBinding(new JettyRestHttpBinding(endpoint));
// disable this filter as we want to use ours
endpoint.setEnableMultipartFilter(false);
}
// configure consumer properties
Consumer consumer = endpoint.createConsumer(processor);
if (config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) {
setProperties(camelContext, consumer, config.getConsumerProperties());
}
// the endpoint must be started before creating the producer
ServiceHelper.startService(endpoint);
return consumer;
}
Aggregations