Пользовательский код Springboot JMS не запускается при развертывании в tomcat как WAR - PullRequest
0 голосов
/ 13 марта 2019

У меня есть следующий потребительский код (от здесь ), который отлично работает, когда я запускаю его в Eclipse следующим образом:

Right Click on Project -->Run As --> Springboot App

Я вижу эту строку кода System.out.println("Received message '" + textMessage.getText(), печатающую сообщение из очереди ActiveMQ.

Однако, когда я развертываю проект как WAR в Tomcat, я не вижу работающего кода. Я имею в виду, что сообщения остаются в очереди и не печатаются. Я ожидал, что, как только я продолжу добавлять сообщения в очередь, он продолжит печатать эти сообщения, но это не работает так. Что я здесь не так делаю?

public class Consumer {


    // URL of the JMS server
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    // default broker URL is : tcp://localhost:61616"

    // Name of the queue we will receive messages from
    private static String subject = "VALLYSOFTQ";

    public static void main(String[] args) throws JMSException {
    // Getting JMS connection from the server
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
    Connection connection = connectionFactory.createConnection();
    connection.start();

    // Creating session for seding messages
    Session session = connection.createSession(false,
    Session.AUTO_ACKNOWLEDGE);

    // Getting the queue 'VALLYSOFTQ'
    Destination destination = session.createQueue(subject);

    // MessageConsumer is used for receiving (consuming) messages
    MessageConsumer consumer = session.createConsumer(destination);

    // Here we receive the message.
    // By default this call is blocking, which means it will wait
    // for a message to arrive on the queue.
    Message message = consumer.receive();

    // There are many types of Message and TextMessage
    // is just one of them. Producer sent us a TextMessage
    // so we must cast to it to get access to its .getText()
    // method.
    if (message instanceof TextMessage) {
    TextMessage textMessage = (TextMessage) message;
    System.out.println("Received message '" + textMessage.getText()
    + "'");
    }
    connection.close();
    }
}

Мой файл pom.xml выглядит следующим образом:

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.myproject.consumer</groupId>
  <artifactId>MyConsumer</artifactId>
  <version>0.0.16</version>  
  <packaging>war</packaging>

  <properties>
      <java.version>1.8</java.version>
      <spring.boot.version>2.0.8.RELEASE</spring.boot.version>
      <spring.orm.version>5.0.11.RELEASE</spring.orm.version>
      <spring.security.version>5.0.11.RELEASE</spring.security.version>
      <hibernate.version>5.3.1.Final</hibernate.version>
      <oracle.jdbc.version>12.1.0.1</oracle.jdbc.version>
      <apache.commons.version>2.1.1</apache.commons.version>
      <junit.version>4.12</junit.version>
      <maven.tomcat.server>mytomcat</maven.tomcat.server>
      <maven.tomcat.url>http://localhost:8080/MyConsumer</maven.tomcat.url>
  </properties>

  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.8.RELEASE</version>
  </parent>

  <dependencies>
      <!-- Spring Boot Starter dependencies below -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-properties-migrator</artifactId>
    <scope>runtime</scope>
</dependency>
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>4.1</version>
        </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
          <scope>provided</scope>         
      </dependency>
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
      <!--  Spring Boot Health -->
      <dependency>
          <groupId>org.springframework.boot</groupId>   
          <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>            

      <!-- Spring ORM/Oracle/JDBC dependencies below -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
      </dependency>
      <dependency>
          <groupId>com.h2database</groupId>
          <artifactId>h2</artifactId>
      </dependency>             
      <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-dbcp2</artifactId>
          <version>${apache.commons.version}</version>
      </dependency>   
      <dependency>
          <groupId>com.oracle</groupId>
          <artifactId>ojdbc7</artifactId>
          <version>${oracle.jdbc.version}</version>       
      </dependency>     
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-orm</artifactId>
          <version>${spring.orm.version}</version>
      </dependency>

       <dependency>
    <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
            <!-- <version>4.2.1.RELEASE</version> -->
            <version>${spring.security.version}</version>
            </dependency>


      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-entitymanager</artifactId>
          <version>${hibernate.version}</version>
      </dependency>



      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
          <version>${hibernate.version}</version>
      </dependency>   

      <!-- JUNIT testing dependencies below -->
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>${junit.version}</version>
      </dependency>   


  </dependencies>

  <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <!-- <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId> -->
          <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <mainClass>com.myproject.consumer.ConsumerWSApp</mainClass>                 
                </configuration>
            </plugin> 
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>{tomcat-maven-plugin.version}</version>
                <configuration>
                    <url>${maven.tomcat.url}</url>
                    <server>${maven.tomcat.server}</server>
                    <path>/${project.artifactId}</path>
                    <update>true</update>                   
                </configuration>
            </plugin>                               
        </plugins>
  </build>

  <repositories>
      <repository>
          <id>spring-releases</id>
          <url>https://repo.spring.io/libs-release</url>
      </repository>
  </repositories>

  <pluginRepositories>
      <pluginRepository>
          <id>spring-releases</id>
          <url>https://repo.spring.io/libs-release</url>
      </pluginRepository>
  </pluginRepositories>  

</project>
...