use of org.apache.camel.builder.RouteBuilder in project camel by apache.
the class BasicPublisherTest method testOnlyOneCamelProducerPerPublisher.
@Test(expected = IllegalStateException.class)
public void testOnlyOneCamelProducerPerPublisher() throws Exception {
new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:one").to("reactive-streams:stream");
from("direct:two").to("reactive-streams:stream");
}
}.addRoutesToCamelContext(context);
context.start();
}
use of org.apache.camel.builder.RouteBuilder in project camel by apache.
the class ShiroAuthorizationTest method createRouteBuilders.
@Override
protected RouteBuilder[] createRouteBuilders() throws Exception {
return new RouteBuilder[] { new RouteBuilder() {
public void configure() {
List<Permission> permissionsList = new ArrayList<Permission>();
Permission permission = new WildcardPermission("zone1:readwrite:*");
permissionsList.add(permission);
final ShiroSecurityPolicy securityPolicy = new ShiroSecurityPolicy("src/test/resources/securityconfig.ini", passPhrase, true, permissionsList);
onException(CamelAuthorizationException.class).to("mock:authorizationException");
from("direct:secureEndpoint").policy(securityPolicy).to("log:incoming payload").to("mock:success");
}
}, new RouteBuilder() {
public void configure() {
List<Permission> permissionsList = new ArrayList<Permission>();
Permission permission = new WildcardPermission("zone1:readonly:*");
permissionsList.add(permission);
permission = new WildcardPermission("zone1:writeonly:*");
permissionsList.add(permission);
final ShiroSecurityPolicy securityPolicy = new ShiroSecurityPolicy("src/test/resources/securityconfig.ini", passPhrase, true, permissionsList);
onException(CamelAuthorizationException.class).to("mock:authorizationException");
from("direct:secureAnyEndpoint").policy(securityPolicy).to("log:incoming payload").to("mock:success");
}
}, new RouteBuilder() {
public void configure() {
List<Permission> permissionsList = new ArrayList<Permission>();
Permission permission = new WildcardPermission("zone1:readonly:*");
permissionsList.add(permission);
permission = new WildcardPermission("zone1:writeonly:*");
permissionsList.add(permission);
final ShiroSecurityPolicy securityPolicy = new ShiroSecurityPolicy("src/test/resources/securityconfig.ini", passPhrase, true, permissionsList);
securityPolicy.setAllPermissionsRequired(true);
onException(CamelAuthorizationException.class).to("mock:authorizationException");
from("direct:secureAllEndpoint").policy(securityPolicy).to("log:incoming payload").to("mock:success");
}
} };
}
use of org.apache.camel.builder.RouteBuilder in project camel by apache.
the class CamelServletContextListener method extractRoutes.
/**
* Extract the routes from the parameters.
*
* @param map parameters
* @return a list of routes, which can be of different types. See source code for more details.
*/
private List<Object> extractRoutes(Map<String, Object> map) {
List<Object> answer = new ArrayList<Object>();
List<String> names = new ArrayList<String>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey().toLowerCase(Locale.UK).startsWith("routebuilder")) {
names.add(entry.getKey());
// we can have multiple values assigned, separated by comma, so create an iterator
String value = (String) entry.getValue();
Iterator<Object> it = ObjectHelper.createIterator(value);
while (it.hasNext()) {
value = (String) it.next();
if (ObjectHelper.isNotEmpty(value)) {
// trim value before usage, as people can indent the values
value = value.trim();
Object target = null;
if (value.startsWith("#")) {
// a reference lookup in jndi
value = value.substring(1);
target = lookupRegistryByName(value);
} else if (ResourceHelper.hasScheme(value)) {
// XML resource from classpath or file system
InputStream is = null;
try {
is = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, value);
target = camelContext.loadRoutesDefinition(is);
} catch (Exception e) {
throw new RuntimeException("Error loading routes from resource: " + value, e);
} finally {
IOHelper.close(is, entry.getKey(), LOG);
}
} else if (value.startsWith("packagescan:")) {
// using package scanning
String path = value.substring(12);
Set<Class<?>> classes = camelContext.getPackageScanClassResolver().findImplementations(RouteBuilder.class, path);
if (!classes.isEmpty()) {
Set<RouteBuilder> builders = new LinkedHashSet<RouteBuilder>();
target = builders;
for (Class<?> clazz : classes) {
try {
RouteBuilder route = (RouteBuilder) camelContext.getInjector().newInstance(clazz);
builders.add(route);
} catch (Exception e) {
throw new RuntimeException("Error creating RouteBuilder " + clazz, e);
}
}
}
} else {
// assume its a FQN classname for a RouteBuilder class
try {
Class<RouteBuilder> clazz = camelContext.getClassResolver().resolveMandatoryClass(value, RouteBuilder.class);
target = camelContext.getInjector().newInstance(clazz);
} catch (Exception e) {
throw new RuntimeException("Error creating RouteBuilder " + value, e);
}
}
if (target != null) {
answer.add(target);
}
}
}
}
}
// after adding the route builders we should remove them from the map
for (String name : names) {
map.remove(name);
}
return answer;
}
use of org.apache.camel.builder.RouteBuilder in project camel by apache.
the class SjmsBatchConsumerTest method testConsumptionCompletionInterval.
@Test
public void testConsumptionCompletionInterval() throws Exception {
final int completionInterval = 2000;
// timeout-based only
final int completionSize = -1;
final String queueName = getQueueName();
context.addRoutes(new TransactedSendHarness(queueName));
context.addRoutes(new RouteBuilder() {
public void configure() throws Exception {
fromF("sjms-batch:%s?completionInterval=%s&completionSize=%s&aggregationStrategy=#testStrategy", queueName, completionInterval, completionSize).routeId("batchConsumer").startupOrder(10).to("mock:batches");
}
});
context.start();
int messageCount = 50;
assertTrue(messageCount < SjmsBatchEndpoint.DEFAULT_COMPLETION_SIZE);
MockEndpoint mockBatches = getMockEndpoint("mock:batches");
// everything ought to be batched together but the interval may trigger in between and we get 2 etc
mockBatches.expectedMinimumMessageCount(1);
template.sendBody("direct:in", generateStrings(messageCount));
mockBatches.assertIsSatisfied();
}
use of org.apache.camel.builder.RouteBuilder in project camel by apache.
the class SjmsBatchConsumerTest method testConsumption.
@Test
public void testConsumption() throws Exception {
final int messageCount = 10000;
final int consumerCount = 5;
final String queueName = getQueueName();
context.addRoutes(new TransactedSendHarness(queueName));
context.addRoutes(new RouteBuilder() {
public void configure() throws Exception {
int completionTimeout = 1000;
int completionSize = 200;
fromF("sjms-batch:%s?completionTimeout=%s&completionSize=%s&consumerCount=%s&aggregationStrategy=#testStrategy", queueName, completionTimeout, completionSize, consumerCount).routeId("batchConsumer").startupOrder(10).autoStartup(false).split(body()).to("mock:split");
}
});
context.start();
MockEndpoint mockBefore = getMockEndpoint("mock:before");
mockBefore.setExpectedMessageCount(messageCount);
MockEndpoint mockSplit = getMockEndpoint("mock:split");
mockSplit.setExpectedMessageCount(messageCount);
LOG.info("Sending messages");
template.sendBody("direct:in", generateStrings(messageCount));
LOG.info("Send complete");
StopWatch stopWatch = new StopWatch();
context.startRoute("batchConsumer");
assertMockEndpointsSatisfied();
long time = stopWatch.stop();
LOG.info("Processed {} messages in {} ms", messageCount, time);
LOG.info("Average throughput {} msg/s", (long) (messageCount / (time / 1000d)));
}
Aggregations