配置文件
package com.ynart.config;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitmqConfig {
private static final String PRODUCT_EXCHANGE = "productExchange";
@Bean
public TopicExchange productExchange() {
return new TopicExchange(PRODUCT_EXCHANGE);
}
@Bean
public Queue sportsQueue() {
return new Queue("sportsQueue");
}
@Bean
public Binding sportsBinding(Queue sportsQueue, TopicExchange productExchange) {
return BindingBuilder.bind(sportsQueue).to(productExchange).with("sports.*");
}
@Bean
public Queue electronicsQueue() {
return new Queue("electronicsQueue");
}
@Bean
public Binding electronicsBinding(Queue electronicsQueue, TopicExchange productExchange) {
return BindingBuilder.bind(electronicsQueue).to(productExchange).with("electronics.*");
}
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
return factory;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
使用
package com.ynart.rabbitmq;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import com.ynart.Dto.ArtDataEntity;
import com.ynart.exedata.domain.ArtData;
import com.ynart.exedata.service.IArtDataService;
import com.ynart.utils.AesUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
@Component
public class RabbitmqService {
private final RabbitTemplate rabbitTemplate;
@Autowired
private IArtDataService artDataService;
@Autowired
private AesUtils aesUtils;
@Autowired
public RabbitmqService(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
public void sendSportsOrder(String message) {
rabbitTemplate.convertAndSend("productExchange", "sports.new", message);
}
public void sendElectronicsOrder(String message) {
rabbitTemplate.convertAndSend("productExchange", "electronics.new", message);
}
@RabbitListener(queues = "sportsQueue")
public void receiveSportsOrder(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws IOException {
try {
message = aesUtils.decryptAES(message);
ObjectMapper objectMapper = new ObjectMapper();
List<ArtDataEntity> list = objectMapper.readValue(message, new TypeReference<List<ArtDataEntity>>() {
});
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
for (ArtDataEntity artDataEntity : list) {
ArtData artData = new ArtData();
artData.setId(null);
artData.setJsonContent(artDataEntity.getJson_content());
artData.setUrl(artDataEntity.getUrl());
artData.setDataVersion(artDataEntity.getData_version());
artData.setSign(artDataEntity.getSign());
artData.setStatus(artDataEntity.getStatus().toString());
artData.setCreatedBy(artDataEntity.getCreated_by());
artData.setCreatedTime(dateFormat.parse(artDataEntity.getCreated_time()));
artData.setUpdatedBy(artDataEntity.getUpdated_by());
artData.setUpdatedTime(dateFormat.parse(artDataEntity.getUpdated_time()));
artDataService.insertArtData(artData);
}
channel.basicAck(deliveryTag, false);
} catch (Exception e) {
System.out.println("Failed to process sports order: " + message);
channel.basicNack(deliveryTag, false, true);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103