Announcement Announcement Module
Collapse
No announcement yet.
getRabbitTemplate throws NullPointerException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • getRabbitTemplate throws NullPointerException

    Following the spring-rabbit-stocks example of the RabbitMarketDataGateway, I've extended my publisher class called EventEnginePublisher with RabbitGatewaySupport. It should pick up the RabbitTemplate from the XML configuration, but it does not. When I publish a message, I get a NullPointerException.

    Here is my XML configuration:

    Code:
      <bean id="eventEnginePublisher" class="eventhub.ws.publish.EventEnginePublisher">
        <property name="rabbitTemplate" ref="amqpTemplate"/>
      </bean>
    
      <rabbit:connection-factory id="connectionFactory" host="eventhub-dev"
                     username="guest" password="guest" channel-cache-size="5"/>
    
      <bean id="jsonConverter" class="org.springframework.amqp.support.converter.JsonMessageConverter"/>
    
      <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
               message-converter="jsonConverter"/>
    
      <rabbit:admin id="rabbitAdmin" connection-factory="connectionFactory"/>
    
      <rabbit:topic-exchange id="eventExchange" name="eventdata" durable="true"/>
    And here is the code that sends the message:

    Code:
    public class EventEnginePublisher extends RabbitGatewaySupport {
      static Logger log = Logger.getLogger(EventEnginePublisher.class);
    
      public publish(EventType event) {
        RabbitTemplate bunnyTemplate = getRabbitTemplate();
    
        ApplicationContext context = new GenericXmlApplicationContext("classpath:/servlet-config.xml");
        RabbitTemplate template = context.getBean(RabbitTemplate.class);
        
        log.debug("bunnyTemplate = " + bunnyTemplate);
        log.debug("     template = " + template);
        bunnyTemplate.convertAndSend("eventexchange", "somerouting", event);
      }
    }
    To compare, I also get the RabbitTemplate using the ApplicationContext. The debug statements produce the following output. The bunnyTemplate is null, but the template references the RabbitTemplate.

    Code:
    2013-04-08 14:58:46,780 DEBUG EventEnginePublisher bunnyTemplate = null
    2013-04-08 14:58:46,782 DEBUG EventEnginePublisher      template = org.springframework.amqp.rabbit.core.RabbitTemplate@64cfa471
    Here is the error:

    Code:
    java.lang.NullPointerException
            eventhub.ws.publish.EventEnginePublisher.publish(EventEnginePublisher.java:91)
            eventhub.ws.publish.PublishEventController.PublishRestEvent(PublishEventController.java:59)
            sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            java.lang.reflect.Method.invoke(Method.java:597)
            org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
            org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
            org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
            org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
            org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
            org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
            org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
            org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
            org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
            org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    EventEnginePublisher.java:91 refers to the code:

    Code:
    bunnyTemplate.convertAndSend("eventexchange", "somerouting", event);
    What am I doing wrong?

  • #2
    First, you don't need to declare a logger, the logger in the superclass is protected.

    Second, you are looking at different contexts; for the second template you are creating a completely new application context.

    So you are not comparing apples with apples.

    How are you getting a handle to the EventEnginePublisher in the PublishEventController?

    You can't just create an instance, you have to inject it into the controller. Please show your complete Spring configuration, and web.xml.

    Comment


    • #3
      Ah! I was missing the injection of the instance of EventEnginePublisher into PublishEventController. I defined it previously using XML, but removed it when I switched to using Java annotations in the PublishEventController to simplify using it as a web service endpoint. I forgot to add the @Autowired annotation to the EventEnginePublisher object, plus I was creating a new instance of EventEnginePublisher every time the web service was hit rather than using the injected EventEnginePublisher. Spring Framework rookie mistakes. Now it works properly and looks like this:

      Code:
      @Controller
      @RequestMapping("/v1")
      final class PublishEventController {
      
        @Autowired
        private EventEnginePublisher eventEnginePublisher;
      
        public void setEventEnginePublisher(EventEnginePublisher eventEnginePublisher) {
          this.eventEnginePublisher = eventEnginePublisher;
        }
      
        @RequestMapping(value = "/rest",
            method = RequestMethod.POST,
            consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE},
            produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
        @ResponseBody
        public EventType PublishRestEvent(@RequestBody EventType publishedEvent) {
      
          publishedEvent.getHead().setTimeStamp(new DateTime());
      
          if (eventEnginePublisher.publish(publishedEvent)) {
            publishedEvent.getHead().setGuid("abcRestEvent123");
          }
          else {
            publishedEvent.getHead().setGuid("error occurred");
          }
          return publishedEvent;
        }
      }
      Thanks for your help!

      Comment

      Working...
      X