@Override public Producer acquireProducer(Endpoint endpoint) { // always create a new producer Producer answer; try { answer = endpoint.createProducer(); if (getCamelContext().isStartingRoutes() && answer.isSingleton()) { // if we are currently starting a route, then add as service and enlist in JMX // - but do not enlist non-singletons in JMX // - note addService will also start the service getCamelContext().addService(answer); } else { // must then start service so producer is ready to be used ServiceHelper.startService(answer); } } catch (Exception e) { throw new FailedToCreateProducerException(endpoint, e); } return answer; }
public void testRoutingSlipCreateProducerFailed() throws Exception { // no inflight assertEquals(0, context.getInflightRepository().size()); template.sendBodyAndHeader("direct:start", "Hello World", "foo", "log:foo"); // no inflight assertEquals(0, context.getInflightRepository().size()); // those 2 options not allowed together try { template.sendBodyAndHeader("direct:start", "Hello World", "foo", "file://target/test?fileExist=Append&tempPrefix=hello"); fail("Should fail"); } catch (CamelExecutionException e) { assertIsInstanceOf(FailedToCreateProducerException.class, e.getCause()); } // no inflight assertEquals(0, context.getInflightRepository().size()); }
@Test public void testCreateSerlvetEndpointProducer() throws Exception { if (!startCamelContext) { // don't test it with web.xml configure return; } try { context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start").to("servlet:///testworld"); } }); fail("Excepts exception here"); } catch (FailedToCreateRouteException ex) { assertTrue("Get a wrong exception.", ex.getCause() instanceof FailedToCreateProducerException); assertTrue("Get a wrong cause of exception.", ex.getCause().getCause() instanceof UnsupportedOperationException); } }
/** * Pre tests the connection before starting the listening. * <p/> * In case of connection failure the exception is thrown which prevents Camel from starting. * * @throws FailedToCreateProducerException is thrown if testing the connection failed */ protected void testConnectionOnStartup() throws FailedToCreateProducerException { try { CamelJmsTemplate template = (CamelJmsTemplate) getInOnlyTemplate(); if (log.isDebugEnabled()) { log.debug("Testing JMS Connection on startup for destination: " + template.getDefaultDestinationName()); } Connection conn = template.getConnectionFactory().createConnection(); JmsUtils.closeConnection(conn); log.debug("Successfully tested JMS Connection on startup for destination: " + template.getDefaultDestinationName()); } catch (Exception e) { throw new FailedToCreateProducerException(getEndpoint(), e); } }
@Test public void testConnectionOnStartupProducerTest() throws Exception { context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start").to("activemq:queue:foo?testConnectionOnStartup=true"); } }); try { context.start(); fail("Should have thrown an exception"); } catch (FailedToCreateProducerException e) { assertTrue(e.getMessage().startsWith("Failed to create Producer for endpoint: activemq://queue:foo?testConnectionOnStartup=true.")); assertTrue(e.getMessage().contains("java.net.ConnectException")); } }
@Test public void clientFailsToConnectUsingWrongCredentialsToNewlyStartedBroker() throws Exception { String user = "someUser"; sut = mqttMain().withBrokerPort(freePort()).withBrokerUser(user) .withBrokerPassword("theBrokersPassword").withClientUser(user) .withClientPassword("notTheBrokersPassword"); exceptions.expect(FailedToCreateProducerException.class); exceptions.expectMessage("CONNECTION_REFUSED_BAD_USERNAME_OR_PASSWORD"); sut.connectToMqttBroker(); }
protected synchronized Producer doGetProducer(Endpoint endpoint, boolean pooled) { String key = endpoint.getEndpointUri(); Producer answer = producers.get(key); if (pooled && answer == null) { // try acquire from connection pool answer = pool.acquire(endpoint); } if (answer == null) { // create a new producer try { answer = endpoint.createProducer(); // add as service which will also start the service // (false => we and handling the lifecycle of the producer in this cache) getCamelContext().addService(answer, false); } catch (Exception e) { throw new FailedToCreateProducerException(endpoint, e); } // add producer to cache or pool if applicable if (pooled && answer instanceof ServicePoolAware) { LOG.debug("Adding to producer service pool with key: {} for producer: {}", endpoint, answer); answer = pool.addAndAcquire(endpoint, answer); } else if (answer.isSingleton()) { LOG.debug("Adding to producer cache with key: {} for producer: {}", endpoint, answer); producers.put(key, answer); } } if (answer != null) { // record statistics if (extendedStatistics) { statistics.onHit(key); } } return answer; }
protected GenericFileProducer<FTPFile> buildProducer() { try { return new RemoteFileProducer<FTPFile>(this, createRemoteFileOperations()); } catch (Exception e) { throw new FailedToCreateProducerException(this, e); } }
@Test public void testInvalidConfiguration() throws Exception { try { template.requestBody("activemq:queue:foo?replyTo=bar&replyToType=Temporary", "Hello World"); fail("Should have thrown exception"); } catch (CamelExecutionException e) { assertIsInstanceOf(FailedToCreateProducerException.class, e.getCause()); assertIsInstanceOf(IllegalArgumentException.class, e.getCause().getCause()); assertEquals("ReplyToType Temporary is not supported when replyTo bar is also configured.", e.getCause().getCause().getMessage()); } }
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 testAuthMethodPriorityInvalid() throws Exception { try { template.requestBody("http://localhost:{{port}}/test?authMethod=Basic&authMethodPriority=Basic,foo&authUsername=donald&authPassword=duck", "Hello World", String.class); fail("Should have thrown an exception"); } catch (FailedToCreateProducerException e) { IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause().getCause().getCause()); //JAXB 2.2 uses a slightly different message boolean b = cause.getMessage().contains("No enum const") && cause.getMessage().contains("org.apache.camel.component.http.AuthMethod.foo"); assertTrue("Bad fault message: " + cause.getMessage(), b); } }
protected void initReplyManager() { if (!started.get()) { synchronized (this) { if (started.get()) { return; } // must use the classloader from the application context when creating reply manager, // as it should inherit the classloader from app context and not the current which may be // a different classloader ClassLoader current = Thread.currentThread().getContextClassLoader(); ClassLoader ac = endpoint.getCamelContext().getApplicationContextClassLoader(); try { if (ac != null) { Thread.currentThread().setContextClassLoader(ac); } // validate that replyToType and replyTo is configured accordingly if (endpoint.getReplyToType() != null) { // setting temporary with a fixed replyTo is not supported if (endpoint.getReplyTo() != null && endpoint.getReplyToType().equals(ReplyToType.Temporary.name())) { throw new IllegalArgumentException("ReplyToType " + ReplyToType.Temporary + " is not supported when replyTo " + endpoint.getReplyTo() + " is also configured."); } } if (endpoint.getReplyTo() != null) { replyManager = createReplyManager(endpoint.getReplyTo()); if (LOG.isDebugEnabled()) { LOG.debug("Using JmsReplyManager: {} to process replies from: {}", replyManager, endpoint.getReplyTo()); } } else { replyManager = createReplyManager(); LOG.debug("Using JmsReplyManager: {} to process replies from temporary queue", replyManager); } } catch (Exception e) { throw new FailedToCreateProducerException(endpoint, e); } finally { if (ac != null) { Thread.currentThread().setContextClassLoader(current); } } started.set(true); } } }
protected void initReplyManager() { if (!started.get()) { synchronized (this) { if (started.get()) { return; } log.debug("Starting reply manager"); // must use the classloader from the application context when creating reply manager, // as it should inherit the classloader from app context and not the current which may be // a different classloader ClassLoader current = Thread.currentThread().getContextClassLoader(); ClassLoader ac = getEndpoint().getCamelContext().getApplicationContextClassLoader(); try { if (ac != null) { Thread.currentThread().setContextClassLoader(ac); } // validate that replyToType and replyTo is configured accordingly if (getEndpoint().getReplyToType() != null) { // setting temporary with a fixed replyTo is not supported if (getEndpoint().getReplyTo() != null && getEndpoint().getReplyToType().equals(ReplyToType.Temporary.name())) { throw new IllegalArgumentException("ReplyToType " + ReplyToType.Temporary + " is not supported when replyTo " + getEndpoint().getReplyTo() + " is also configured."); } } if (getEndpoint().getReplyTo() != null) { // specifying reply queues is not currently supported throw new IllegalArgumentException("Specifying replyTo " + getEndpoint().getReplyTo() + " is currently not supported."); } else { replyManager = createReplyManager(); log.debug("Using RabbitMQReplyManager: {} to process replies from temporary queue", replyManager); } } catch (Exception e) { throw new FailedToCreateProducerException(getEndpoint(), e); } finally { if (ac != null) { Thread.currentThread().setContextClassLoader(current); } } started.set(true); } } }