2017-12-12 7 views
0

널 포인터 :메시지 보내기주고 내가 프로젝트를 실행 때 나는이 오류 얻을에/MVC 봄의 ActiveMQ에서 샘플 전송을 실행하고 메시지를 받아 봐하려고 생각 예외

java.lang.NullPointerException 
    com.sarf.jms.MessageProducerBean.sendMessage(MessageProducerBean.java:24) 
    com.sarf.testactimq.HomeController.home(HomeController.java:42) 
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    java.lang.reflect.Method.invoke(Method.java:497) 
    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.doGet(FrameworkServlet.java:778) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 

내 소스 코드 :

MessageObject.java :

package com.sarf.data; 

public class MessageObject { 
    private String mailId; 
    private String message; 

public MessageObject(){}; 
public MessageObject(String mailId, String message) { 
    super(); 
    this.mailId = mailId; 
    this.message = message; 
} 
public String getMailId() { 
    return mailId; 
} 
public void setMailId(String mailId) { 
    this.mailId = mailId; 
} 
public String getMessage() { 
    return message; 
} 
public void setMessage(String message) { 
    this.message = message; 
} 
} 

MessageConsumerBean.java :

01 23,186,115,767,434,452,762,479,453,210

MessageProducerBean.java :

package com.sarf.jms; 
import javax.jms.Destination; 
import javax.jms.JMSException; 
import javax.jms.MapMessage; 
import javax.jms.Message; 
import javax.jms.Session; 

import org.springframework.jms.core.JmsTemplate; 
import org.springframework.jms.core.MessageCreator; 
import com.sarf.data.MessageObject; 

public class MessageProducerBean { 

    //JMS Template object 
    private JmsTemplate jmsTemplate; 
    private Destination destination; 
    public void setJmsTemplate(JmsTemplate jmsTemplate) { 
     this.jmsTemplate = jmsTemplate; 
    } 
    public void setDestination(Destination destination) { 
     this.destination = destination; 
    } 
    public void sendMessage(final MessageObject messageObj) { 
     jmsTemplate.send(destination, new MessageCreator() { 
     public Message createMessage(Session session) throws JMSException { 
      MapMessage message = session.createMapMessage(); 
      message.setString("mailId", messageObj.getMailId()); 
      message.setString("message", messageObj.getMessage()); 
      return message; 
      } 
     }); //send method close here 
    }//method ends here 
    } 

Homecontroller :

package com.sarf.testactimq; 

import java.text.DateFormat; 

import java.util.Date; 
import java.util.Locale; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import com.sarf.data.MessageObject; 
import com.sarf.jms.MessageProducerBean; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.sarf.jms.*; 

/** 
* Handles requests for the application home page. 
*/ 
@Controller 
public class HomeController { 

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class); 

    /** 
    * Simply selects the home view to render by returning its name. 
    */ 
    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String home(Locale locale, Model model) { 
     logger.info("Welcome home! The client locale is {}.", locale); 

     Date date = new Date(); 
     DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); 

     String formattedDate = dateFormat.format(date); 

     model.addAttribute("serverTime", formattedDate); 
     MessageProducerBean mp = new MessageProducerBean(); 
     mp.sendMessage(new MessageObject("1234", "Test Message")); 
      /* 
     MessageConsumerBean mc = new MessageConsumerBean(); 
       MessageObject messageObj = mc.receiveMessage(); 
       System.out.println("Message from " + 
       messageObj.getMailId() + " received"); 
     */ 
     return "home"; 
    } 

} 

servlete_context.xml :

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

      <beans:bean id="connectionFactory" 
     class="org.apache.activemq.ActiveMQConnectionFactory"> 
     <beans:property name="brokerURL" value="tcp://localhost:61616" /> 
    </beans:bean> 

    <beans:bean id="mailDestination" 
     class="org.apache.activemq.command.ActiveMQQueue"> 
     <beans:constructor-arg value="mail.queue" /> 
    </beans:bean> 

    <beans:bean id="jmsTemplate" 
     class="org.springframework.jms.core.JmsTemplate"> 
     <beans:property name="connectionFactory" ref="connectionFactory" /> 
    </beans:bean> 

    <beans:bean id="producer" 
     class="com.sarf.jms.MessageProducerBean"> 
     <beans:property name="destination" ref="mailDestination" /> 
     <beans:property name="jmsTemplate" ref="jmsTemplate" /> 
    </beans:bean> 
    <beans:bean id="consumer" 
     class="com.sarf.jms.MessageConsumerBean"> 
     <beans:property name="destination" ref="mailDestination" /> 
     <beans:property name="jmsTemplate" ref="jmsTemplate" /> 
    </beans:bean> 

    <context:component-scan base-package="com.sarf.testactimq" /> 



</beans:beans> 

root_context.xml :

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <!-- Root Context: defines shared resources visible to all other web components --> 
      <bean id="connectionFactory" 
     class="org.apache.activemq.ActiveMQConnectionFactory"> 
     <property name="brokerURL" value="tcp://localhost:61616" /> 
    </bean> 

    <bean id="mailDestination" 
     class="org.apache.activemq.command.ActiveMQQueue"> 
     <constructor-arg value="mail.queue" /> 
    </bean> 

    <bean id="jmsTemplate" 
     class="org.springframework.jms.core.JmsTemplate"> 
     <property name="connectionFactory" ref="connectionFactory" /> 
    </bean> 

    <bean id="producer" 
     class="com.sarf.jms.MessageProducerBean"> 
     <property name="destination" ref="mailDestination" /> 
     <property name="jmsTemplate" ref="jmsTemplate" /> 
    </bean> 
    <bean id="consumer" 
     class="com.sarf.jms.MessageConsumerBean"> 
     <property name="destination" ref="mailDestination" /> 
     <property name="jmsTemplate" ref="jmsTemplate" /> 
    </bean> 
</beans> 

의 pom.xml :

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.sarf</groupId> 
    <artifactId>testactimq</artifactId> 
    <name>testmessage</name> 
    <packaging>war</packaging> 
    <version>1.0.0-BUILD-SNAPSHOT</version> 
    <properties> 
     <java-version>1.6</java-version> 
     <org.springframework-version>3.1.1.RELEASE</org.springframework-version> 
     <org.aspectj-version>1.6.10</org.aspectj-version> 
     <org.slf4j-version>1.6.6</org.slf4j-version> 
    </properties> 
    <dependencies> 

    <dependency> 
     <groupId>org.apache.activemq</groupId> 
     <artifactId>activemq-all</artifactId> 
     <version>5.13.3</version> 
    </dependency> 
     <!-- Spring --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>${org.springframework-version}</version> 
      <exclusions> 
       <!-- Exclude Commons Logging in favor of SLF4j --> 
       <exclusion> 
        <groupId>commons-logging</groupId> 
        <artifactId>commons-logging</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>${org.springframework-version}</version> 
     </dependency> 

     <!-- AspectJ --> 
     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjrt</artifactId> 
      <version>${org.aspectj-version}</version> 
     </dependency> 

     <!-- Logging --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>${org.slf4j-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>jcl-over-slf4j</artifactId> 
      <version>${org.slf4j-version}</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
      <version>${org.slf4j-version}</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>1.2.15</version> 
      <exclusions> 
       <exclusion> 
        <groupId>javax.mail</groupId> 
        <artifactId>mail</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>javax.jms</groupId> 
        <artifactId>jms</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>com.sun.jdmk</groupId> 
        <artifactId>jmxtools</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>com.sun.jmx</groupId> 
        <artifactId>jmxri</artifactId> 
       </exclusion> 
      </exclusions> 
      <scope>runtime</scope> 
     </dependency> 

     <!-- @Inject --> 
     <dependency> 
      <groupId>javax.inject</groupId> 
      <artifactId>javax.inject</artifactId> 
      <version>1</version> 
     </dependency> 

     <!-- Servlet --> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>2.5</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>jsp-api</artifactId> 
      <version>2.1</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 

     <!-- Test --> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.7</version> 
      <scope>test</scope> 
     </dependency>   
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-eclipse-plugin</artifactId> 
       <version>2.9</version> 
       <configuration> 
        <additionalProjectnatures> 
         <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> 
        </additionalProjectnatures> 
        <additionalBuildcommands> 
         <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> 
        </additionalBuildcommands> 
        <downloadSources>true</downloadSources> 
        <downloadJavadocs>true</downloadJavadocs> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.5.1</version> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
        <compilerArgument>-Xlint:all</compilerArgument> 
        <showWarnings>true</showWarnings> 
        <showDeprecation>true</showDeprecation> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>exec-maven-plugin</artifactId> 
       <version>1.2.1</version> 
       <configuration> 
        <mainClass>org.test.int1.Main</mainClass> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

내 컨트롤러에서 호출 MessageProducerBean에서 정의한 메도 sendMessage 첨부, 내 ActiveMQ를 실행 호출에 의한 실행 시간 오류 이미지가 아래와 같이 enter image description here 참고 : 나는 tc 서버 및 activemq 버전 5.13.3 과 함께 spring suite 도구를 사용하므로 어떤 도움을 받고 있습니까? HomeController에서

+0

왜 당신은'HomeController'에서'MessageProducerBean'과'MessageConsumerBean'을 인스턴스화하는 대신 autowiring하려고합니까? 이로 인해 컨트롤러에서 사용할 때 jmsTemplate과 대상이 모두 null입니다. 또한 bean 정의는 root_context.xml과 servlet_context.xml 파일에 복제됩니다. – Setu

답변

0

당신은 MessageProducerBean 같은 만드는 :

MessageProducerBean mp = new MessageProducerBean(); 

과 즉시 메시지를 보낼 수있는 MessageProducerBean를 사용하여 : 그러나

mp.sendMessage(new MessageObject("1234", "Test Message")); 

을,이 시점에서 MessageProducerBean이를 사용하려고 jmsTemplate 초기화하지 않은 변수이므로 null입니다. 따라서 java.lang.NullPointerException을 받게됩니다.

관련 문제