如何等到@Jmslistner消费者接到电话?

时间:2019-08-26 04:45:49

标签: spring-boot activemq consumer

我的代码中同时包含生产者和消费者。生产者正在向队列中生成一条消息,而使用者正在使用该消息。

问题在生产者将消息发送到队列后不久,便调用了一些其他代码,而该消费者获得消息并将消息放入列表之后。 (在控制器中的jmsProducer.send(customer)被调用之后不久;列出客户= customerStorage.getAll()被调用。我需要等到消费者消费jmsProducer.send(customer)之后的消息。)

我想等到“其他代码部分”,直到使用者使用该消息为止。如何实现?

感谢您的帮助。

制作人

@Component
public class JmsProducer {

    @Autowired
    JmsTemplate jmsTemplate;

    @Value("${gkz.activemq.queue}")
    String queue;

    public void send(Customer customer){
        jmsTemplate.convertAndSend(queue, customer);
    }
}

消费者

@Component
public class JmsConsumer {
    @Autowired
    private MessageStorage customerStorage;

    @JmsListener(destination = "${gkz.activemq.queue}",containerFactory="jsaFactory")
    public void receive(Customer customer){
        System.out.println("Recieved Message: " + customer);
        customerStorage.add(customer);
    }
}

控制器

@PostMapping(value="/api/customer")
public Customer postCustomer(@RequestBody Customer customer){
    jmsProducer.send(customer);
        List<Customer> customers = customerStorage.getAll();
        //send email
    return customer;
}

MessageStorage

public class MessageStorage {
    private List<Customer> customers = new ArrayList<>();

    public void add(Customer customer) {
        customers.add(customer);
    }

    public void clear() {
        customers.clear();
    }

    public List<Customer> getAll(){
        return customers;
    }
}

ConnectionFactoryConfiguration

@Configuration
public class ConnectionFactoryConfiguration {

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

    @Value("${gkz.activemq.borker.username}")
    String userName;

    @Value("${gkz.activemq.borker.password}")
    String password;

    @Bean
    public ConnectionFactory connectionFactory(){
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(brokerUrl);
        connectionFactory.setUserName(userName);
        connectionFactory.setPassword(password);
        return connectionFactory;
    }

    @Bean
    public MessageConverter jacksonJmsMessageConverter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setTargetType(MessageType.TEXT);
        converter.setTypeIdPropertyName("_type");
        return converter;
    }


    //Used for Receiving Message
    @Bean
    public JmsListenerContainerFactory<?> jsaFactory(ConnectionFactory connectionFactory,
                                                    DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setMessageConverter(jacksonJmsMessageConverter());
        configurer.configure(factory, connectionFactory);
        return factory;
    }


    //Used for Sending Messages.
    @Bean
    public JmsTemplate jmsTemplate(){
        JmsTemplate template = new JmsTemplate();
        template.setMessageConverter(jacksonJmsMessageConverter());
        template.setConnectionFactory(connectionFactory());
        return template;
    }

}

0 个答案:

没有答案