MqConsumer.java
3.33 KB
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
package cn.com.glsx.consumer.common;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.DeadLetterPolicy;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.SubscriptionType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MqConsumer {
private static final Logger logger = LoggerFactory.getLogger(MqConsumer.class);
private String serviceUrl;
private String accessId;
private String accessKey;
private String topic;
private int maxRedeliverCount;
private IMessageListener messageListener;
private String subscriptionName;
public static MqConsumer build() {
return new MqConsumer();
}
public MqConsumer serviceUrl(String serviceUrl) {
this.serviceUrl = serviceUrl;
return this;
}
public MqConsumer accessId(String accessId) {
this.accessId = accessId;
return this;
}
public MqConsumer subscriptionName(String subscriptionName) {
this.subscriptionName = subscriptionName;
return this;
}
public MqConsumer accessKey(String accessKey) {
this.accessKey = accessKey;
return this;
}
public MqConsumer topic(String topic) {
this.topic = topic;
return this;
}
public MqConsumer maxRedeliverCount(int maxRedeliverCount) {
this.maxRedeliverCount = maxRedeliverCount;
return this;
}
public MqConsumer messageListener(IMessageListener messageListener) {
this.messageListener = messageListener;
return this;
}
public void start() throws Exception {
if (serviceUrl == null || serviceUrl.trim().length() == 0) {
throw new IllegalStateException("serviceUrl must be initialized");
}
if (accessId == null || accessId.trim().length() == 0) {
throw new IllegalStateException("accessId must be initialized");
}
if (accessKey == null || accessKey.trim().length() == 0) {
throw new IllegalStateException("accessKey must be initialized");
}
if (messageListener == null) {
throw new IllegalStateException("messageListener must be initialized");
}
PulsarClient client = PulsarClient.builder().serviceUrl(serviceUrl).allowTlsInsecureConnection(true)
.authentication(new MqAuthentication(accessId, accessKey)).build();
Consumer consumer = client.newConsumer().topic(String.format("%s/out/%s", accessId, topic))
.subscriptionName(String.format("%s-sub", subscriptionName)).subscriptionType(SubscriptionType.Failover)
.deadLetterPolicy(DeadLetterPolicy.builder().maxRedeliverCount(maxRedeliverCount).build()).subscribe();
do {
try {
Message message = consumer.receive();
Long s = System.currentTimeMillis();
messageListener.onMessageArrived(message);
logger.info("business processing cost={}", System.currentTimeMillis() - s);
consumer.acknowledge(message);
} catch (Throwable t) {
logger.error("error:", t);
}
} while (true);
}
public interface IMessageListener {
void onMessageArrived(Message message) throws Exception;
}
}