doum
2025-12-11 3cd92951fd2a67a02e649a870d100b3e8776ae11
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
package com.doumee.config.rocketmq5;
 
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.rocketmq.client.apis.ClientConfiguration;
import org.apache.rocketmq.client.apis.ClientException;
import org.apache.rocketmq.client.apis.ClientServiceProvider;
import org.apache.rocketmq.client.apis.SessionCredentialsProvider;
import org.apache.rocketmq.client.apis.StaticSessionCredentialsProvider;
import org.apache.rocketmq.client.apis.message.Message;
import org.apache.rocketmq.client.apis.producer.Producer;
import org.apache.rocketmq.client.apis.producer.SendReceipt;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
 
@Service
@Slf4j
public class NormalMsgProducer {
    @Autowired
    public   MqConfig mqConfig;
 
    public   boolean sendMsg(String tag,String msg)   {
        try {
            final ClientServiceProvider provider = ClientServiceProvider.loadService();
            StaticSessionCredentialsProvider staticSessionCredentialsProvider =
                    new StaticSessionCredentialsProvider(mqConfig.getUsername(), mqConfig.getPassword());
            String endpoints = mqConfig.getNameSrvAddr();
            ClientConfiguration clientConfiguration = ClientConfiguration.newBuilder()
                    .setEndpoints(endpoints)
                    .setCredentialProvider(staticSessionCredentialsProvider)
                    .build();
            String topic = mqConfig.getTopic();
            // In most case, you don't need to create too many producers, singleton pattern is recommended.
            final Producer producer = provider.newProducerBuilder()
                    .setClientConfiguration(clientConfiguration)
                    // Set the topic name(s), which is optional but recommended. It makes producer could prefetch the topic
                    // route before message publishing.
                    .setTopics(topic)
                    // May throw {@link ClientException} if the producer is not initialized.
                    .build();
            // Define your message body.
            byte[] body = msg.getBytes(StandardCharsets.UTF_8);
            final Message message = provider.newMessageBuilder()
                    // Set topic for the current message.
                    .setTopic(topic)
                    // Message secondary classifier of message besides topic.
                    .setTag(tag)
                    // Key(s) of the message, another way to mark message besides message id.
//                    .setKeys(null)
                    .setBody(body)
                    .build();
            final SendReceipt sendReceipt = producer.send(message);
            log.info("Send message successfully, messageId={}", sendReceipt.getMessageId());
            // Close the producer when you don't need it anymore.
            producer.close();
            return  true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  false;
    }
 
}