-
Notifications
You must be signed in to change notification settings - Fork 63
/
ProduceConsume.java
executable file
·150 lines (119 loc) · 4.64 KB
/
ProduceConsume.java
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.messagebus.scenario.client;
import com.messagebus.client.IMessageReceiveListener;
import com.messagebus.client.Messagebus;
import com.messagebus.client.MessagebusSinglePool;
import com.messagebus.client.message.model.Message;
import com.messagebus.client.message.model.MessageFactory;
import com.messagebus.common.Constants;
import com.messagebus.scenario.util.PropertiesHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Created by yanghua on 2/23/15.
*/
public class ProduceConsume {
private static final Log logger = LogFactory.getLog(ProduceConsume.class);
public static void main(String[] args) {
produce();
consumeWithPushStyle();
//or
// consumeWithPullStyle();
//or async consume
// asyncConsume();
}
private static void produce() {
String secret = "kljasdoifqoikjhhhqwhebasdfasdf";
String token = "hlkasjdhfkqlwhlfalksjdhgssssas";
MessagebusSinglePool singlePool = new MessagebusSinglePool(
PropertiesHelper.getPropertyValue("messagebus.pubsuber.host")
);
Messagebus client = singlePool.getResource();
Message msg = MessageFactory.createMessage();
msg.setContentType("text/plain");
msg.setContentEncoding("utf-8");
msg.setContent("test".getBytes(Constants.CHARSET_OF_UTF8));
client.produce(secret, "emapDemoConsume", msg, token);
singlePool.returnResource(client);
singlePool.destroy();
}
private static void consumeWithPullStyle() {
String secret = "zxdjnflakwenklasjdflkqpiasdfnj";
MessagebusSinglePool singlePool = new MessagebusSinglePool(
PropertiesHelper.getPropertyValue("messagebus.pubsuber.host")
);
Messagebus client = singlePool.getResource();
List<Message> msgs = client.consume(secret, 1);
for (Message msg : msgs) {
logger.info(msg.getMessageId());
}
singlePool.returnResource(client);
singlePool.destroy();
}
private static void consumeWithPushStyle() {
String secret = "zxdjnflakwenklasjdflkqpiasdfnj";
MessagebusSinglePool singlePool = new MessagebusSinglePool(
PropertiesHelper.getPropertyValue("messagebus.pubsuber.host")
);
Messagebus client = singlePool.getResource();
client.consume(secret, 10, TimeUnit.SECONDS, new IMessageReceiveListener() {
@Override
public void onMessage(Message message) {
logger.info(message.getMessageId());
}
});
singlePool.returnResource(client);
singlePool.destroy();
}
private static void asyncConsume() {
AsyncConsumeThread asyncConsumeThread = new AsyncConsumeThread();
asyncConsumeThread.startup();
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
asyncConsumeThread.shutdown();
}
private static class AsyncConsumeThread implements Runnable {
private Thread currentThread;
private MessagebusSinglePool singlePool;
private Messagebus client;
public AsyncConsumeThread() {
this.currentThread = new Thread(this);
this.currentThread.setName("AsyncConsumeThread");
this.currentThread.setDaemon(true);
}
@Override
public void run() {
String secret = "zxdjnflakwenklasjdflkqpiasdfnj";
singlePool = new MessagebusSinglePool(
PropertiesHelper.getPropertyValue("messagebus.pubsuber.host")
);
client = singlePool.getResource();
//register notification listener
client.setNotificationListener(new IMessageReceiveListener() {
@Override
public void onMessage(Message message) {
logger.info("received notification : " + message.getAppId());
}
});
//long long time
client.consume(secret, Integer.MAX_VALUE, TimeUnit.SECONDS, new IMessageReceiveListener() {
@Override
public void onMessage(Message message) {
logger.info(message.getMessageId());
}
});
}
public void startup() {
this.currentThread.start();
}
public void shutdown() {
singlePool.returnResource(client);
singlePool.destroy();
this.currentThread.interrupt();
}
}
}