This topic describes how to push events from Message Queue for RabbitMQ to Function Compute by using EventBridge.
Prerequisites
- EventBridge
- Function Compute
- Message Queue for RabbitMQ
Step 1: Create a custom event source
- Log on to the EventBridge console.
- In the left-side navigation pane, click Event Buses.
- In the top navigation bar, select a region.
- On the Event Buses page, click the name of a custom event bus.
- In the left-side navigation pane, click Event Sources.
- On the Event Source page, click Add Event Source.
- In the Add Custom Event Source panel, set the Name and Description parameters, select Message Queue for RabbitMQ from the Event Provider drop-down list, select the resource information about Message Queue for RabbitMQ, and then click OK.
Step 2: Create an event rule
- Log on to the EventBridge console.
- In the left-side navigation pane, click Event Buses.
- In the top navigation bar, select a region.
- On the Event Buses page, click the name of the event bus that you want to manage.
- In the left-side navigation pane, click Event Rules.
- On the Event Rules page, click Create Rule.
- Complete the Create Rule wizard.
Step 3: Publish an event
import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;
import java.util.HashMap;
import java.util.UUID;
public class ProducerTest {
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
// Specify the endpoint. You can view the endpoint of your instance on the Instance Details page in the Message Queue for RabbitMQ console.
factory.setHost("xxx.xxx.aliyuncs.com");
// Specify the username. You can view the username on the Static Accounts page in the Message Queue for RabbitMQ console.
factory.setUsername("${UserName}");
// Specify the password that corresponds to the username. You can view the password on the Static Accounts page in the Message Queue for RabbitMQ console.
factory.setPassword("${PassWord}");
// Enable automatic connection recovery. If you set the value to true, automatic connection recovery is enabled. If you set the value to false, automatic connection recovery is disabled.
factory.setAutomaticRecoveryEnabled(true);
factory.setNetworkRecoveryInterval(5000);
// Specify the vhost name. Make sure that the vhost has been created in the Message Queue for RabbitMQ console.
factory.setVirtualHost("${VhostName}");
// Specify the default port. Use port 5672 for non-encrypted connections and port 5671 for encrypted connections.
factory.setPort(5672);
// Set a timeout period based on the network environment.
factory.setConnectionTimeout(30 * 1000);
factory.setHandshakeTimeout(30 * 1000);
factory.setShutdownTimeout(0);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare("${ExchangeName}", "${ExchangeType}", true, false, false, null);
channel.queueDeclare(${"QueueName}", true, false, false, new HashMap<String, Object>());
channel.queueBind(${"QueueName}", "${ExchangeName}", "${BindingKey});
// Send a message.
for (int i = 0; i < 100; i++ ) {
// Set ${ExchangeName} to an exchange that already exists in the Message Queue for RabbitMQ console. Make sure that the type of the exchange is consistent with that in the console.
// Set ${BindingKey} to the corresponding binding key based on your business requirements.
AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().messageId(UUID.randomUUID().toString()).build();
channel.basicPublish("${ExchangeName}", "BindingKey", true, props,
("messageBody" + i).getBytes(StandardCharsets.UTF_8));
}
connection.close();
}
}
Verify the result
To verify the result, you can view logs in the Function Compute console.
FAQ
If an event fails to be published, you can view the response to the publishing request for troubleshooting. You can go to the EventBridge console and view the related information in the Event Delivery section of the Event Trace message. Then, take appropriate measures based on the response returned.
What can I do if an event fails to be published to Function Compute and the "[500]ConnectErrorconnectiontimedout" error is returned in the response?
- Log on to the Function Compute console. Execute the function to which the event is routed and check the execution duration.
- If the execution duration is longer than 15s, check the network connection. If the execution duration is shorter than 15s, check whether you can access the endpoint for the region where the service to which the event is routed is deployed.
- If you cannot access the endpoint, contact Function Compute engineers to seek help.