protected XmlModel assertRoutes(File file, int expected, String ns) throws Exception { if (ns == null || ns.trim().length() == 0) { ns = CamelNamespaces.springNS; } XmlModel x = assertLoadModel(file, expected); // now lets add a route and write it back again... DefaultCamelContext tmpContext = new DefaultCamelContext(); tmpContext.addRoutes( new RouteBuilder() { @Override public void configure() throws Exception { from("seda:newFrom").to("seda:newTo"); } }); x.getContextElement().getRoutes().addAll(tmpContext.getRouteDefinitions()); List<RouteDefinition> routes = x.getRouteDefinitionList(); assertEquals("routes: " + routes, expected + 1, routes.size()); // now lets write to XML model outDir.mkdirs(); File outFile = new File(outDir, file.getName()); System.out.println("Generating file: " + outFile); tool.marshal(outFile, x); assertFileExists(outFile); // lets check the file has the correct namespace inside it String text = FileCopyUtils.copyToString(new FileReader(outFile)); assertTrue("Namespace " + ns + " not present in output file\n" + text, text.contains(ns)); return x; }
protected CamelContext createCamelContext(String name, String managementPattern) throws Exception { DefaultCamelContext context = new DefaultCamelContext(); context.setName(name); if (managementPattern != null) { context.getManagementNameStrategy().setNamePattern(managementPattern); } return context; }
protected void startCamelContext() throws Exception { if (camelContextService != null) { camelContextService.start(); } else { if (context instanceof DefaultCamelContext) { DefaultCamelContext defaultCamelContext = (DefaultCamelContext) context; if (!defaultCamelContext.isStarted()) { defaultCamelContext.start(); } } else { context.start(); } } }
public void testRouteStartupOrder() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); // assert correct order DefaultCamelContext dcc = (DefaultCamelContext) context; List<RouteStartupOrder> order = dcc.getRouteStartupOrder(); assertEquals(4, order.size()); assertEquals("seda://foo", order.get(0).getRoute().getEndpoint().getEndpointUri()); assertEquals("direct://start", order.get(1).getRoute().getEndpoint().getEndpointUri()); assertEquals("seda://bar", order.get(2).getRoute().getEndpoint().getEndpointUri()); assertEquals("direct://bar", order.get(3).getRoute().getEndpoint().getEndpointUri()); }
@Reference public void setConnectionFactory(ConnectionFactory connectionFactory) { try { rwl.writeLock().lock(); if (camelContext == null) { LOG.info("Creating the Camel Context"); camelContext = new DefaultCamelContext(); LOG.info("Activating the SjmsComponent"); SjmsComponent sjms = getConnectionFactoryInstance(connectionFactory); camelContext.addComponent("sjms", sjms); LOG.info("Starting the Camel Context"); camelContext.start(); } } catch (Exception e) { LOG.error("Error starting up the Camel Context", e); } finally { rwl.writeLock().unlock(); } }
public CamelContext createCamelContext() throws Exception { DefaultCamelContext camelContext = (DefaultCamelContext) super.createCamelContext(); INT26_Registry registry = new INT26_Registry(camelContext.getRegistry()); // CXF_PAYLOAD_HEADER_FILTER bean CxfConsumerSoapHeaderFilter cxfConsumerSoapHeaderFilter = new CxfConsumerSoapHeaderFilter(); registry.register("CXF_PAYLOAD_HEADER_FILTER", cxfConsumerSoapHeaderFilter); // extractException bean beans.ExtractExceptionBean extractExceptionBean = new beans.ExtractExceptionBean(); registry.register("extractException", extractExceptionBean); // jsonErrorGenerator bean beans.JSONFaultGeneratorBean jsonFaultGeneratorBean = new JSONFaultGeneratorBean(); registry.register("jsonErrorGenerator", jsonFaultGeneratorBean); camelContext.setRegistry(registry); return camelContext; }
public void unsetConnectionFactory(ConnectionFactory connectionFactory) { try { rwl.writeLock().lock(); LOG.info(" Yes, shut it down."); camelContext.stop(); camelContext = null; } catch (Exception e) { // e.printStackTrace(); LOG.error("Error shutting down the Camel Context", e); } finally { rwl.writeLock().unlock(); } }
@Override protected JndiRegistry createRegistry() throws Exception { JndiRegistry registry = super.createRegistry(); // lets create our black box as a camel context and a set of routes DefaultCamelContext blackBox = new DefaultCamelContext(registry); blackBox.setName("blackBox"); blackBox.addRoutes( new RouteBuilder() { @Override public void configure() throws Exception { // receive purchase orders, lets process it in some way then // send an invoice to our invoice endpoint from("direct:purchaseOrder") .setHeader("received") .constant("true") .to("direct:invoice"); } }); blackBox.start(); registry.bind("accounts", blackBox); return registry; }
@Activate public synchronized void activate(Map<?, ?> properties) throws Exception { LOG.info("Activating: " + COMPONENT_LABEL); try { rwl.writeLock().lock(); LOG.info(properties.toString()); LOG.info("Activating the Producer Endpoint"); producer = camelContext.createProducerTemplate(); producer.setDefaultEndpointUri((String) properties.get("endpointUri")); producer.start(); } finally { rwl.writeLock().unlock(); } }
public void onContextStart(CamelContext context) throws VetoCamelContextStartException { Object mc = getManagementObjectStrategy().getManagedObjectForCamelContext(context); String name = context.getName(); String managementName = context.getManagementNameStrategy().getName(); try { boolean done = false; while (!done) { ObjectName on = getManagementStrategy() .getManagementNamingStrategy() .getObjectNameForCamelContext(managementName, name); boolean exists = getManagementStrategy().isManaged(mc, on); if (!exists) { done = true; } else { // okay there exists already a CamelContext with this name, we can try to fix it by // finding a free name boolean fixed = false; // if we use the default name strategy we can find a free name to use String newName = findFreeName(mc, context.getManagementNameStrategy(), name); if (newName != null) { // use this as the fixed name fixed = true; done = true; managementName = newName; } // we could not fix it so veto starting camel if (!fixed) { throw new VetoCamelContextStartException( "CamelContext (" + context.getName() + ") with ObjectName[" + on + "] is already registered." + " Make sure to use unique names on CamelContext when using multiple CamelContexts in the same MBeanServer.", context); } else { LOG.warn( "This CamelContext(" + context.getName() + ") will be registered using the name: " + managementName + " due to clash with an existing name already registered in MBeanServer."); } } } } catch (VetoCamelContextStartException e) { // rethrow veto throw e; } catch (Exception e) { // must rethrow to allow CamelContext fallback to non JMX agent to allow // Camel to continue to run throw ObjectHelper.wrapRuntimeCamelException(e); } // set the name we are going to use if (context instanceof DefaultCamelContext) { ((DefaultCamelContext) context).setManagementName(managementName); } try { manageObject(mc); } catch (Exception e) { // must rethrow to allow CamelContext fallback to non JMX agent to allow // Camel to continue to run throw ObjectHelper.wrapRuntimeCamelException(e); } // yes we made it and are initialized initialized = true; if (mc instanceof ManagedCamelContext) { camelContextMBean = (ManagedCamelContext) mc; } // register any pre registered now that we are initialized enlistPreRegisteredServices(); }
public void start() throws Exception { DefaultCamelContext context = new DefaultCamelContext(); context.addRoutes(getRoute()); addBeans(context); context.start(); }
private void addBeans(DefaultCamelContext camel) throws Exception { JndiContext jc = new JndiContext(); jc.bind("tap", new LogMessage()); camel.setJndiContext(jc); }