Skip to content

Commit

Permalink
增加rabbitmq配置,以持久化方式投递消息 (alibaba#4644)
Browse files Browse the repository at this point in the history
Co-authored-by: wangheng <760261296@qq.com>
  • Loading branch information
2 people authored and zoemak committed Jan 30, 2024
1 parent 68e0df4 commit 8ef89dd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class RabbitMQConstants {
public static final String RABBITMQ_VIRTUAL_HOST = ROOT + "." + "virtual.host";
public static final String RABBITMQ_USERNAME = ROOT + "." + "username";
public static final String RABBITMQ_PASSWORD = ROOT + "." + "password";
public static final String RABBITMQ_QUEUE = ROOT + "." + "queue";
public static final String RABBITMQ_ROUTING_KEY = ROOT + "." + "routingKey";
public static final String RABBITMQ_DELIVERY_MODE = ROOT + "." + "deliveryMode";

public static final String RABBITMQ_RESOURCE_OWNERID = ROOT + "." + "rabbitmq.resource.ownerId";
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class RabbitMQProducerConfig extends MQProperties {
private String exchange;
private String username;
private String password;
private String queue;
private String routingKey;
private String deliveryMode;

public String getHost() {
return host;
Expand Down Expand Up @@ -55,4 +58,28 @@ public String getPassword() {
public void setPassword(String password) {
this.password = password;
}

public String getQueue() {
return queue;
}

public void setQueue(String queue) {
this.queue = queue;
}

public String getRoutingKey() {
return routingKey;
}

public void setRoutingKey(String routingKey) {
this.routingKey = routingKey;
}

public String getDeliveryMode() {
return deliveryMode;
}

public void setDeliveryMode(String deliveryMode) {
this.deliveryMode = deliveryMode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Properties;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -26,10 +27,6 @@
import com.alibaba.otter.canal.connector.rabbitmq.config.RabbitMQProducerConfig;
import com.alibaba.otter.canal.protocol.FlatMessage;
import com.alibaba.otter.canal.protocol.Message;
import com.rabbitmq.client.AlreadyClosedException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
* RabbitMQ Producer SPI 实现
Expand Down Expand Up @@ -75,7 +72,10 @@ public void init(Properties properties) {
try {
connect = factory.newConnection();
channel = connect.createChannel();
// channel.exchangeDeclare(mqProperties.getExchange(), "topic");
channel.queueDeclare(rabbitMQProperties.getQueue(), true, false, false, null);
channel.exchangeDeclare(rabbitMQProperties.getExchange(), rabbitMQProperties.getDeliveryMode(), true, false, false, null);
channel.queueBind(rabbitMQProperties.getQueue(), rabbitMQProperties.getExchange(), rabbitMQProperties.getRoutingKey());

} catch (IOException | TimeoutException ex) {
throw new CanalException("Start RabbitMQ producer error", ex);
}
Expand Down Expand Up @@ -106,6 +106,18 @@ private void loadRabbitMQProperties(Properties properties) {
if (!StringUtils.isEmpty(password)) {
rabbitMQProperties.setPassword(password);
}
String queue = PropertiesUtils.getProperty(properties, RabbitMQConstants.RABBITMQ_QUEUE);
if (!StringUtils.isEmpty(queue)) {
rabbitMQProperties.setQueue(queue);
}
String routingKey = PropertiesUtils.getProperty(properties, RabbitMQConstants.RABBITMQ_ROUTING_KEY);
if (!StringUtils.isEmpty(routingKey)) {
rabbitMQProperties.setRoutingKey(routingKey);
}
String deliveryMode = PropertiesUtils.getProperty(properties, RabbitMQConstants.RABBITMQ_DELIVERY_MODE);
if (!StringUtils.isEmpty(deliveryMode)) {
rabbitMQProperties.setDeliveryMode(deliveryMode);
}
}

@Override
Expand Down Expand Up @@ -165,7 +177,7 @@ private void sendMessage(String queueName, byte[] message) {
// tips: 目前逻辑中暂不处理对exchange处理,请在Console后台绑定 才可使用routekey
try {
RabbitMQProducerConfig rabbitMQProperties = (RabbitMQProducerConfig) this.mqProperties;
channel.basicPublish(rabbitMQProperties.getExchange(), queueName, null, message);
channel.basicPublish(rabbitMQProperties.getExchange(), queueName, MessageProperties.PERSISTENT_TEXT_PLAIN, message);
} catch (Throwable e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 8ef89dd

Please sign in to comment.