2017-12-12 8 views
0

を与える私は、MVC、スプリングによって/ ActiveMQのからメッセージを送信し、reciveサンプルを実行しようとしています。しかし、私はこのエラーを取得するプロジェクトを実行するとき:送信メッセージMVC春にはActiveMQにnullポインタ例外

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:

package com.sarf.jms; 

import javax.jms.Destination; 
import javax.jms.JMSException; 
import javax.jms.MapMessage; 

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

public class MessageConsumerBean{ 

    private JmsTemplate jmsTemplate; 
    private Destination destination; 

    public void setJmsTemplate(JmsTemplate jmsTemplate) { 
     this.jmsTemplate = jmsTemplate; 
    } 
    public void setDestination(Destination destination) { 
     this.destination = destination; 
    } 

    public MessageObject receiveMessage() { 
     MapMessage message = (MapMessage) jmsTemplate.receive(destination); 
     try { 
      MessageObject messageObj = new MessageObject(); 
      messageObj.setMailId(message.getString("mailId")); 
      messageObj.setMessage(message.getString("message")); 
      return messageObj; 
      } catch (JMSException e) { 
      throw JmsUtils.convertJmsAccessException(e); 
      } 
     } 
    } 

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で定義MethodeののsendMessageが、私は私のActiveMQを実行する呼び出しによって引き起こされ、実行時にエラー画像が下図のように: enter image description here 注:私はtcサーバーとactivemqバージョン5.13.3 でspringスイートツールを使用していますので、どうしたらいいですか?

あなたがそうのような MessageProducerBean作成している HomeController
+0

なぜあなたは 'HomeController'で' MessageProducerBean'と 'MessageConsumerBean'をインスタンス化しようとしていますか?このため、コントローラから使用しようとすると、jmsTemplateとdestinationは両方ともnullになります。また、Bean定義は、root_context.xmlファイルとservlet_context.xmlファイルに複製されます。 – Setu

答えて

0

:メッセージを送信するためにMessageProducerBeanを使用して、その後すぐ

MessageProducerBean mp = new MessageProducerBean(); 

とを:

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

しかし、この時点でMessageProducerBeanが使用しようとしていますjmsTemplate初期化していない変数なので、nullです。したがって、java.lang.NullPointerExceptionが届きます。