No results found. Please try re-entering the product name
Compute
Database
Office Automation
Middleware
Partners
Intelligent Management
Backup
Big Data
Management & Monitoring
Customer service
Network&CDN
Communication
IT Management
General Reference
This chapter describes how to send messages with SDK by taking calling Java SDK in TCP protocol as an example.
After completing the creation of topics, producers, consumers and other resources through the console, you need to integrate the SDK into the client application and complete the code of the business logic for sending messages.
com.paic.fincloud
fmq-client
1.0.0
com.alibaba
fastjson
1.2.3
io.netty
netty-all
4.0.25.Final
org.slf4j
slf4j-api
1.7.5
public static Producer initProducer() throws FCException {
// Prepare required parameters.
Properties propertie = new Properties();
// The business system can take out the properties value from the configuration file and write it in the demo.
propertie.setProperty(FCConstant.PRODUCER_ID, "PID-demo");// Must configure, and create in the console.
propertie.setProperty(FCConstant.NAME_SERVER_ADDRESS, "NAME_SERVER_ADDR-demo");// Must configure, environment IP address.
// Set ak/sk, get it from the console.
propertie.setProperty(FCConstant.ACCESS_KEY, "ACCESSKEY-demo");
propertie.setProperty(FCConstant.SECRET_KEY, "SECRETKEY-demo");
// Get MQCPProducer instance, producer is single-instance。
Producer producer = FCFactory.createProducer(propertie);
// Start service
producer.start();
return producer;
}
public static void sendMsg(Producer producer) {
try {
String topic = Constant.TOPIC;// Send messages. If you need to send messages to different topics, just send them to different topics.
FCMessage message = TestProducer.generateMsg(topic);
// Send messages.
// The status is returned on success, In case of failure, an exception will be thrown directly.
FCSendResult sendResult = producer.sendMessage(message);
// We recommend that you add some logs in the business side to record the status of sending messages, such as message ID, message tag, message key, and message sending status.
System.out.println("####msg status:" + sendResult.getSendStatus() + " msgId:" + sendResult.getMessageId());
} catch (FCException ex) {
// TODO: If failed, the business system can add some exception handling. But we do not recommend that you send messages again, as internal sendmsg of mqcp has a retry mechanism.
// Retry for 3 times, and the polling broker is used to retry. The retry time does not exceed 10s by default. However, if it is a timeout exception, it will not retry.
// It is recommended that the business system be stored in the database and retry periodically from the backend.
ex.printStackTrace();
}
}
Call us
400-151-8800
Email us
cloud@pingan.com