@Test public void testPojoRoutes() throws Exception { JndiContext jndctx = new JndiContext(); jndctx.bind("bye", new SayService("Good Bye!")); CamelContext camelContext = new DefaultCamelContext(jndctx); camelContext.addRoutes(createRouteBuilder(camelContext)); camelContext.start(); try { Endpoint endpoint = camelContext.getEndpoint("direct:hello"); ISay proxy = ProxyHelper.createProxy(endpoint, false, ISay.class); String rc = proxy.say(); Assert.assertEquals("Good Bye!", rc); } finally { camelContext.stop(); } }
/** * Creates the object to be injected for an {@link org.apache.camel.EndpointInject} or {@link org.apache.camel.Produce} injection point */ public Object getInjectionValue(Class<?> type, String endpointUri, String endpointRef, String endpointProperty, String injectionPointName, Object bean, String beanName) { if (type.isAssignableFrom(ProducerTemplate.class)) { return createInjectionProducerTemplate(endpointUri, endpointRef, endpointProperty, injectionPointName, bean); } else if (type.isAssignableFrom(ConsumerTemplate.class)) { return createInjectionConsumerTemplate(endpointUri, endpointRef, endpointProperty, injectionPointName); } else { Endpoint endpoint = getEndpointInjection(bean, endpointUri, endpointRef, endpointProperty, injectionPointName, true); if (endpoint != null) { if (type.isInstance(endpoint)) { return endpoint; } else if (type.isAssignableFrom(Producer.class)) { return createInjectionProducer(endpoint, bean, beanName); } else if (type.isAssignableFrom(PollingConsumer.class)) { return createInjectionPollingConsumer(endpoint, bean, beanName); } else if (type.isInterface()) { // lets create a proxy try { return ProxyHelper.createProxy(endpoint, type); } catch (Exception e) { throw createProxyInstantiationRuntimeException(type, endpoint, e); } } else { throw new IllegalArgumentException("Invalid type: " + type.getName() + " which cannot be injected via @EndpointInject/@Produce for: " + endpoint); } } return null; } }
@Test public void testJavaSpaceRequestReply() throws Exception { Endpoint endpoint = context.getEndpoint("direct:input"); ITestPojo proxy = ProxyHelper.createProxy(endpoint, ITestPojo.class); Request req = new Request(); long start = System.currentTimeMillis(); for (int i = 0; i < 100; ++i) { req.setPayload("REQUEST " + i); Reply reply = proxy.method(req); assertTrue(reply.getPayload().equals("REPLY for REQUEST " + i)); } long stop = System.currentTimeMillis(); log.info("{} took {} milliseconds", getTestMethodName(), stop - start); }
public void afterPropertiesSet() throws Exception { if (endpoint == null) { getCamelContext(); if (getServiceUrl() == null && getServiceRef() == null) { throw new IllegalArgumentException("serviceUrl or serviceRef must be specified."); } if (getServiceInterface() == null) { throw new IllegalArgumentException("serviceInterface must be specified."); } // lookup endpoint or we have the url for it if (getServiceRef() != null) { endpoint = getCamelContext().getRegistry().lookupByNameAndType(getServiceRef(), Endpoint.class); } else { endpoint = getCamelContext().getEndpoint(getServiceUrl()); } if (endpoint == null) { throw new IllegalArgumentException("Could not resolve endpoint: " + getServiceUrl()); } } // binding is enabled by default boolean bind = getBinding() != null ? getBinding() : true; try { // need to start endpoint before we create producer ServiceHelper.startService(endpoint); producer = endpoint.createProducer(); // add and start producer getCamelContext().addService(producer, true, true); Class<?> clazz = blueprintContainer.loadClass(getServiceInterface()); serviceProxy = ProxyHelper.createProxy(endpoint, bind, producer, clazz); } catch (Exception e) { throw new FailedToCreateProducerException(endpoint, e); } }
@Override public void afterPropertiesSet() { if (endpoint == null) { if (ObjectHelper.isNotEmpty(camelContextId)) { camelContext = CamelContextResolverHelper.getCamelContextWithId(applicationContext, camelContextId); } if (camelContext == null) { throw new IllegalArgumentException("camelContext or camelContextId must be specified"); } if (getServiceUrl() == null && getServiceRef() == null) { throw new IllegalArgumentException("serviceUrl or serviceRef must be specified."); } // lookup endpoint or we have the url for it if (getServiceRef() != null) { endpoint = camelContext.getRegistry().lookupByNameAndType(getServiceRef(), Endpoint.class); } else { endpoint = camelContext.getEndpoint(getServiceUrl()); } if (endpoint == null) { throw new IllegalArgumentException("Could not resolve endpoint: " + getServiceUrl()); } } // binding is enabled by default boolean bind = getBinding() != null ? getBinding() : true; try { // need to start endpoint before we create producer ServiceHelper.startService(endpoint); producer = endpoint.createProducer(); // add and start producer camelContext.addService(producer, true, true); serviceProxy = ProxyHelper.createProxy(endpoint, bind, producer, getServiceInterface()); } catch (Exception e) { throw new FailedToCreateProducerException(endpoint, e); } }
@Test public void testPojoRoutes() throws Exception { if (classPathHasSpaces()) { return; } // Boot up a local RMI registry LocateRegistry.createRegistry(getPort()); // START SNIPPET: register JndiContext context = new JndiContext(); context.bind("bye", new SayService("Good Bye!")); CamelContext camelContext = new DefaultCamelContext(context); // END SNIPPET: register camelContext.addRoutes(getRouteBuilder(camelContext)); camelContext.start(); // START SNIPPET: invoke Endpoint endpoint = camelContext.getEndpoint("direct:hello"); ISay proxy = ProxyHelper.createProxy(endpoint, false, ISay.class); String rc = proxy.say(); assertEquals("Good Bye!", rc); // END SNIPPET: invoke camelContext.stop(); }
@Produces @Named("calculatorProxy") public Calculator calculator(CamelContext context) throws Exception { Endpoint endpoint = context.getEndpoint("direct:calculatorProxy"); Calculator proxy = ProxyHelper.createProxy(endpoint, Calculator.class); return proxy; }
public void testEcho() throws Exception { Echo service = ProxyHelper.createProxy(context.getEndpoint("direct:echo"), Echo.class); assertEquals("Hello World", service.echo("Hello World")); }
public void testEchoNull() throws Exception { Echo service = ProxyHelper.createProxy(context.getEndpoint("direct:echo"), Echo.class); assertEquals(null, service.echo(null)); }
/** * Builds the proxy. * * @param interfaceClasses the service interface(s) * @return the proxied bean * @throws Exception is thrown if error creating the proxy */ public <T> T build(Class<T>... interfaceClasses) throws Exception { ObjectHelper.notNull(endpoint, "endpoint"); return ProxyHelper.createProxy(endpoint, binding, interfaceClasses); }