You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Camel Guy (JIRA)" <ji...@apache.org> on 2014/11/24 19:35:15 UTC

[jira] [Comment Edited] (CAMEL-8077) NullPointerException in getRouteDefinition before context is started

    [ https://issues.apache.org/jira/browse/CAMEL-8077?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14223256#comment-14223256 ] 

Camel Guy edited comment on CAMEL-8077 at 11/24/14 6:34 PM:
------------------------------------------------------------

Yes, I am using 2.14.1-SNAPSHOT.

route.getId() returns null when the context has not started.

{noformat}
public synchronized RouteDefinition getRouteDefinition(String id) {
    for (RouteDefinition route : routeDefinitions) {
        if (route.getId().equals(id)) {
            return route;
        }
    }
    return null;
}
{noformat}

There was a time when I could get one route via getRouteDefinition by id successfully but not another. getRouteDefinition(A) = successful but getRouteDefinition(B) = exception. The difference is that route B is declared in camel-context.xml after route A.

However, after adding <import> and <routeContextRef>, getRouteDefinition(A) failed too. The route I'm trying to grab is in the main context file, not in the imported one.

It appears that camel-context.xml is loaded in a background thread and has a race condition with my methods. I have the same problem with @Test methods. I tried sleeping for, say, a minute in @Begin and @Test and that doesn't fix the problem. The only fix is to call context.start().

Here's my test.java

{noformat}
package my.test;

import org.apache.camel.EndpointInject;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.test.spring.CamelSpringTestSupport;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.junit.Before;
import org.junit.Test;

public final class OneJDBCErrorTest extends CamelSpringTestSupport {
	protected AbstractXmlApplicationContext createApplicationContext() {
		return new ClassPathXmlApplicationContext("META-INF/spring/camel-context.xml");
	}

	@EndpointInject(uri="direct:Start")
	private ProducerTemplate start;

	@EndpointInject(uri="mock:Test:JDBC:TotalExceptions")
	private MockEndpoint mockTotalExceptions;

	@EndpointInject(uri="mock:Test:JDBC:Failed")
	private MockEndpoint mockJdbcFailed;

	@Override
	public boolean isUseAdviceWith() {
	    return true;
	}

	@Before
	final public void jdbcError() throws Exception {
		context.start(); // bug in 2.14 - this is needed
		
		ModelCamelContext model = (ModelCamelContext) context;

		RouteDefinition jdbc = model.getRouteDefinition("SQL:JDBC");
		
		jdbc.adviceWith(context, new AdviceWithRouteBuilder() {
	        @Override
	        public void configure() throws Exception {
	        	weaveById("test:SQL:JDBC:NextError").before().to("mock:Test:JDBC:TotalExceptions");
	        }
	    }
		);
	}
	
	@Test
	final public void oneFailure() throws Exception {
		mockTotalExceptions.expectedMessageCount(4);
		mockJdbcFailed.expectedMessageCount(0);
		
		context.start();
		start.sendBody("");

		mockTotalExceptions.assertIsSatisfied();
		mockJdbcFailed.assertIsSatisfied();

		context.stop();
	}
}
{noformat}



was (Author: camelguy):
Yes, I am using 2.14.1-SNAPSHOT.

route.getId() returns null when the context has not started. There was a time when I could get one route by id successfully but not another, with no clear difference between the two.

{noformat}
public synchronized RouteDefinition getRouteDefinition(String id) {
    for (RouteDefinition route : routeDefinitions) {
        if (route.getId().equals(id)) {
            return route;
        }
    }
    return null;
}
{noformat}

However, after adding <import> and <routeContextRef>, that failed too. The route I'm trying to grab is in the main context file, not in the imported one.

It appears that camel-context.xml is loaded in a background thread and has a race condition with my @Before methods. I have the same problem with @Test methods. I tried sleeping for, say, a minute in @Begin and @Test and that doesn't fix the problem. The only fix is to call context.start().

Here's my test.java

{noformat}
package my.test;

import org.apache.camel.EndpointInject;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.test.spring.CamelSpringTestSupport;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.junit.Before;
import org.junit.Test;

public final class OneJDBCErrorTest extends CamelSpringTestSupport {
	protected AbstractXmlApplicationContext createApplicationContext() {
		return new ClassPathXmlApplicationContext("META-INF/spring/camel-context.xml");
	}

	@EndpointInject(uri="direct:Start")
	private ProducerTemplate start;

	@EndpointInject(uri="mock:Test:JDBC:TotalExceptions")
	private MockEndpoint mockTotalExceptions;

	@EndpointInject(uri="mock:Test:JDBC:Failed")
	private MockEndpoint mockJdbcFailed;

	@Override
	public boolean isUseAdviceWith() {
	    return true;
	}

	@Before
	final public void jdbcError() throws Exception {
		context.start(); // bug in 2.14 - this is needed
		
		ModelCamelContext model = (ModelCamelContext) context;

		RouteDefinition jdbc = model.getRouteDefinition("SQL:JDBC");
		
		jdbc.adviceWith(context, new AdviceWithRouteBuilder() {
	        @Override
	        public void configure() throws Exception {
	        	weaveById("test:SQL:JDBC:NextError").before().to("mock:Test:JDBC:TotalExceptions");
	        }
	    }
		);
	}
	
	@Test
	final public void oneFailure() throws Exception {
		mockTotalExceptions.expectedMessageCount(4);
		mockJdbcFailed.expectedMessageCount(0);
		
		context.start();
		start.sendBody("");

		mockTotalExceptions.assertIsSatisfied();
		mockJdbcFailed.assertIsSatisfied();

		context.stop();
	}
}
{noformat}


> NullPointerException in getRouteDefinition before context is started
> --------------------------------------------------------------------
>
>                 Key: CAMEL-8077
>                 URL: https://issues.apache.org/jira/browse/CAMEL-8077
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-core
>    Affects Versions: 2.14.1
>            Reporter: Camel Guy
>
> Not sure if this occurs in 2.14.0. Does not occur in 2.13.3.
> I am extending CamelSpringTestSupport with:
> @Override
> public boolean isUseAdviceWith() {
>     return true;
> }
> In a @Before method I call context.getRouteDefintion("some.id")
> It throws a NullPointerException:
> org.apache.camel.impl.DefaultCamelContext.getRouteDefinition(DefaultCamelContext.java:1464)
> If I put context.start() at the top of the @Before method, it works.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)