rabbitmq direct消息发送

一、配置
package com.czq.amqp.rabbitmqdemo.config.amqp;

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

@Configuration
public class AmqpConfig {

@Value("${spring.rabbitmq.addresses}")
private String addresses;

@Value("${spring.rabbitmq.virtual-host}")
private String virtualHost;

@Value("${spring.rabbitmq.username}")
private String userName;

@Value("${spring.rabbitmq.password}")
private String password;

public static final String  PERSON_CONNECTIONFACTORY = "personConnectionFactory";
public static final String  PERSON_LISTENERCONNECTIONFACTORY = "personListenerConnectionFactory";

@Bean(PERSON_CONNECTIONFACTORY)
@Primary
public ConnectionFactory lendConnectionFactory(){
    CachingConnectionFactory connectionFactory = createConnectionFactory(addresses, virtualHost, userName, password);
    return connectionFactory;
}

protected CachingConnectionFactory createConnectionFactory(String addresses, String virtualHost, String userName, String password){
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setAddresses(addresses);
    connectionFactory.setVirtualHost(virtualHost);
    connectionFactory.setUsername(userName);
    connectionFactory.setPassword(password);
    return connectionFactory;
}

@Bean(PERSON_LISTENERCONNECTIONFACTORY)
public SimpleRabbitListenerContainerFactory lendListenerConnectionFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
                                                                          @Qualifier(PERSON_CONNECTIONFACTORY) ConnectionFactory connectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    configurer.configure(factory, connectionFactory);
    return factory;
}

@Bean("rabbitTemplate")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public RabbitTemplate rabbitTemplate(@Qualifier(PERSON_CONNECTIONFACTORY) ConnectionFactory lendConnectionFactory){
    RabbitTemplate rabbitTemplate = new RabbitTemplate(lendConnectionFactory);
    return rabbitTemplate;
}

public static final String EXCHANGE_PERSON = “exchange.person”;
public static final String QUEUE_TEST_PERSON = “queue.test.person”;
public static final String PERSON_TEST_KEY = “routingkey.queue.test.person.to.test”;

@Bean
public Queue queueTestPerson(){
    return new Queue(QUEUE_TEST_PERSON);
}

@Bean
public DirectExchange exchangePerson(){
    return new DirectExchange(EXCHANGE_PERSON);
}

@Bean
public Binding bindingExamineToPreQueueToLendExchange(Queue queueTestPerson,
                                                      DirectExchange exchangePerson){
    return BindingBuilder.bind(queueTestPerson).to(exchangePerson).with(PERSON_TEST_KEY);
}

}

二、发送
package com.czq.amqp.rabbitmqdemo.controller;

import com.czq.amqp.rabbitmqdemo.config.amqp.AmqpPersonConfig;
import com.czq.amqp.rabbitmqdemo.config.amqp.AmqpPublicConfig;
import com.czq.amqp.rabbitmqdemo.service.LendMqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**

  • Created by Administrator on 2019/4/8.
    */
    @RestController
    public class RabbitController {

    @Autowired
    private LendMqService lendMqService;

    @RequestMapping("/send")
    public String send(){
    lendMqService.send(AmqpPersonConfig.QUEUE_TEST_PERSON, “hello world!”);
    return “ok”;
    }
    }

package com.czq.amqp.rabbitmqdemo.service;

import com.czq.amqp.rabbitmqdemo.config.amqp.AmqpPersonConfig;
import com.czq.amqp.rabbitmqdemo.config.amqp.AmqpPublicConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class LendMqService extends LendMqAbstractService{

private static final Logger LOGGER = LoggerFactory.getLogger(LendMqService.class);

@Autowired
@Qualifier("rabbitTemplate")
private RabbitTemplate rabbitTemplate;

public void send(String queue, String context){
    rabbitTemplate.convertAndSend(AmqpPersonConfig.EXCHANGE_PERSON, AmqpPersonConfig.PERSON_TEST_KEY, context);
}

}

三、接收
package com.czq.amqp.rabbitmqdemo.listener;

import com.czq.amqp.rabbitmqdemo.config.amqp.AmqpConfig;
import com.czq.amqp.rabbitmqdemo.config.amqp.AmqpPersonConfig;
import com.czq.amqp.rabbitmqdemo.config.amqp.AmqpPublicConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitmqConsumerListener {

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

@RabbitListener(queues = AmqpPersonConfig.QUEUE_TEST_PERSON, containerFactory = AmqpConfig.PERSON_LISTENERCONNECTIONFACTORY)
@RabbitHandler
public void test(String lendRequestId) {
    logger.info("receive test message is {}",lendRequestId);
}

}

接收结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RabbitMQ-C是一个轻量级的RabbitMQ客户端库,它是用C语言编写的,专为那些需要低级别控制或在资源受限的环境中操作AMQP(Advanced Message Queuing Protocol)的应用设计。它提供了直接访问RabbitMQ服务器的功能,包括发送和接收消息。 在RabbitMQ-C中发送消息的基本步骤如下: 1. **初始化连接**:首先,你需要创建一个连接到RabbitMQ服务器的连接实例,通常通过`amqp_connection_new()`创建。 ```c amqp_connection_t *connection = amqp_connection_new(amqp_uri_new("amqp://localhost"), NULL); ``` 2. **声明交换器和队列**:如果消息需要路由到特定的队列,你需要声明这些资源。 ```c amqp_exchange_t *exchange = amqp_exchange_declare(connection, "your_exchange", AMQP_EX_TYPE_DIRECT, 0, NULL, NULL, NULL); amqp_queue_t *queue = amqp_queue_declare(connection, "your_queue", 0, NULL, NULL, NULL, NULL); ``` 3. **绑定队列到交换器**:确保队列接收到来自交换器的消息,使用`amqp_queue_bind()`方法。 ```c amqp_queue_bind(connection, queue, exchange, "your_routing_key", NULL, NULL); ``` 4. **构建消息**:定义一个消息体(含数据、头部信息),例如使用`amqp_message_new()`创建一个新的AMQP消息。 ```c amqp_message_t *message = amqp_message_new(0, NULL); amqp_basic_publish_props_t *props = amqp_basic_publish_props_new(); ``` 5. **设置消息属性**:填充消息头,如交换器、路由键等,并设置消息持久化选项。 ```c amqp_message_set_properties(message, props, AMQP_CMSG_NONE, "your_exchange", "your_routing_key", NULL, 0, NULL); amqp_basic_publish(connection, exchange, message, AMQP_NOPARAM, NULL, NULL, NULL); ``` 6. **发送消息**:调用`amqp_basic_publish()`方法将消息发送到交换器。 7. **清理**:记得关闭连接、交换器、队列和消息资源。 ```c amqp_message_destroy(message); amqp_queue_delete(connection, queue); amqp_exchange_delete(connection, exchange); amqp_connection_close(connection); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值