Потребитель не получает сообщение, когда два микросервиса обмениваются данными при весенней загрузке - PullRequest
0 голосов
/ 26 декабря 2018

Потребитель не получает сообщений, когда два микросервиса обмениваются данными при весенней загрузке.Сообщения создаются производителем, и я вижу их по этому "http://localhost:8161/admin/queues.jsp" URL-адресу, но когда я запускаю получатель, он не получает никаких сообщений.

Приложение Springboot

package com.accenture.messaging.standaloneactivemqexample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StandaloneActivemqExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(StandaloneActivemqExampleApplication.class, args);
    }

}

Producer.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;

@RestController
@RequestMapping("/rest/publish")
public class ProducerResource {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Queue queue;


    @GetMapping("/{message}")
    public String publish(@PathVariable("message") final String message){

    jmsTemplate.convertAndSend(queue,message);

    return "Submitted Successfully";
}

}

Config.java

package com.accenture.messaging.standaloneactivemqexample.resource;


package com.accenture.messaging.standaloneactivemqexample.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.Queue;

@Configuration
public class Config {

    @Value("${activemq.broker-url}")
    private String brokerUrl;

    @Bean
    public Queue queue(){
                return new ActiveMQQueue("standalone1.queue");
            }

     // Need to use ActiveMQConnectionFactory as we are using standalone MQ

     @Bean
    public ActiveMQConnectionFactory activeMQConnectionFactory(){
         ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
         factory.setBrokerURL(brokerUrl);
         return factory;
     }

     @Bean
    public JmsTemplate jmsTemplate(){
        return new JmsTemplate(activeMQConnectionFactory());
     }
}

application.properties для производителя

spring.activemq.in-memory=false
spring.activemq.pool.enabled=false
server.port=8082
activemq.broker-url=tcp://localhost:61616

Потребительское загрузочное приложение

package com.accenture.secondmicroservice.inmemoryqueuesecondmicroservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class InmemoryQueueSecondmicroserviceApplication {

    public static void main(String[] args) {
        SpringApplication.run(InmemoryQueueSecondmicroserviceApplication.class, args);
    }

}

consumer.java

package ResourceConsumer;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

    @JmsListener(destination="standalone1.queue")
    public void consume(String message) {

        System.out.println("Received message : " + message);
    }
}

Config.java (на стороне потребителя)

package config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.Queue;

@Configuration
public class Config {

    @Value("${activemq.broker-url}")
    private String brokerUrl;

//    @Bean
//    public Queue queue(){
//        return new ActiveMQQueue("standalone1.queue");
//    }

    // Need to use ActiveMQConnectionFactory as we are using standalone MQ

    @Bean
    public ActiveMQConnectionFactory activeMQConnectionFactory(){
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
        factory.setBrokerURL(brokerUrl);
        return factory;
    }

    @Bean
    public JmsTemplate jmsTemplate(){
        return new JmsTemplate(activeMQConnectionFactory());
    }
}

application.properties (потребитель)

 spring.activemq.in-memory=false
 spring.activemq.pool.enabled=false
 server.port=8083
 activemq.broker-url=tcp://localhost:61616 

Потребитель должен получать сообщения

...