Java源码示例:io.lettuce.core.pubsub.StatefulRedisPubSubConnection
示例1
@Test
public void testPubSub(final MockTracer tracer) {
final StatefulRedisPubSubConnection<String,String> connection = client.connectPubSub();
connection.addListener(new RedisPubSubAdapter<>());
final RedisPubSubCommands<String,String> commands = connection.sync();
commands.subscribe("channel");
final RedisCommands<String,String> commands2 = client.connect().sync();
commands2.publish("channel", "msg");
await().atMost(15, TimeUnit.SECONDS).until(TestUtil.reportedSpansSize(tracer), equalTo(4));
client.shutdown();
final List<MockSpan> spans = tracer.finishedSpans();
assertEquals(4, spans.size());
}
示例2
@Test
public void pubSub() {
RedisClient client = RedisClient.create("redis://localhost");
StatefulRedisPubSubConnection<String, String> connection =
new TracingStatefulRedisPubSubConnection<>(client.connectPubSub(),
new TracingConfiguration.Builder(mockTracer).build());
connection.addListener(new RedisPubSubAdapter<>());
RedisPubSubCommands<String, String> commands = connection.sync();
commands.subscribe("channel");
final RedisCommands<String, String> commands2 = client.connect().sync();
commands2.publish("channel", "msg");
client.shutdown();
await().atMost(15, TimeUnit.SECONDS).until(reportedSpansSize(), equalTo(3));
List<MockSpan> spans = mockTracer.finishedSpans();
assertEquals(3, spans.size());
}
示例3
@Test
public void pubSub() {
RedisClient client = RedisClient.create("redis://localhost");
StatefulRedisPubSubConnection<String, String> connection =
new TracingStatefulRedisPubSubConnection<>(client.connectPubSub(),
new TracingConfiguration.Builder(mockTracer).build());
connection.addListener(new RedisPubSubAdapter<>());
RedisPubSubCommands<String, String> commands = connection.sync();
commands.subscribe("channel");
final RedisCommands<String, String> commands2 = client.connect().sync();
commands2.publish("channel", "msg");
client.shutdown();
await().atMost(15, TimeUnit.SECONDS).until(reportedSpansSize(), equalTo(3));
List<MockSpan> spans = mockTracer.finishedSpans();
assertEquals(3, spans.size());
}
示例4
@Test
public void pubSub() {
RedisClient client = RedisClient.create("redis://localhost");
StatefulRedisPubSubConnection<String, String> connection =
new TracingStatefulRedisPubSubConnection<>(client.connectPubSub(),
new TracingConfiguration.Builder(mockTracer).build());
connection.addListener(new RedisPubSubAdapter<>());
RedisPubSubCommands<String, String> commands = connection.sync();
commands.subscribe("channel");
final RedisCommands<String, String> commands2 = client.connect().sync();
commands2.publish("channel", "msg");
client.shutdown();
await().atMost(15, TimeUnit.SECONDS).until(reportedSpansSize(), equalTo(3));
List<MockSpan> spans = mockTracer.finishedSpans();
assertEquals(3, spans.size());
}
示例5
@Test
public void givenPubSubChannel_whenMessage_thenMessageReceived() throws Exception {
Listener listener = new Listener();
StatefulRedisPubSubConnection<String, String> connection = redisClient.connectPubSub();
StatefulRedisPubSubConnection<String, String> pubconnection = redisClient.connectPubSub();
connection.addListener(listener);
RedisPubSubAsyncCommands<String, String> async = connection.async();
async.subscribe("channel");
RedisPubSubAsyncCommands<String, String> pubasync = pubconnection.async();
RedisFuture<Long> result = pubasync.publish("channel", "hithere");
// Need a long wait for publish to complete, depending on system.
result.get(15, TimeUnit.SECONDS);
assertTrue(listener.getMessage().equals("hithere"));
}
示例6
private void subscribeFromChannel(String channel) {
StatefulRedisPubSubConnection<String, String> pubSubConnection = redisClient.connectPubSub();
RedisPubSubAdapter<String, String> adapterListener = new DelegatingRedisPubSubListener();
pubSubConnection.addListener(adapterListener);
RedisPubSubCommands<String, String> sync = pubSubConnection.sync();
sync.subscribe(channel);
}
示例7
@Test
public void testPubMsgAndReceiveSuccess() {
List<FlowRule> rules = FlowRuleManager.getRules();
Assert.assertEquals(1, rules.size());
int maxQueueingTimeMs = new Random().nextInt();
StatefulRedisPubSubConnection<String, String> connection = client.connectPubSub();
String flowRules =
"[{\"resource\":\"test\", \"limitApp\":\"default\", \"grade\":1, \"count\":\"0.0\", \"strategy\":0, "
+ "\"refResource\":null, "
+
"\"controlBehavior\":0, \"warmUpPeriodSec\":10, \"maxQueueingTimeMs\":" + maxQueueingTimeMs
+ ", \"controller\":null}]";
RedisPubSubCommands<String, String> subCommands = connection.sync();
subCommands.multi();
subCommands.set(ruleKey, flowRules);
subCommands.publish(channel, flowRules);
subCommands.exec();
await().timeout(2, TimeUnit.SECONDS)
.until(new Callable<List<FlowRule>>() {
@Override
public List<FlowRule> call() throws Exception {
return FlowRuleManager.getRules();
}
}, Matchers.hasSize(1));
rules = FlowRuleManager.getRules();
Assert.assertEquals(rules.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
String value = subCommands.get(ruleKey);
List<FlowRule> flowRulesValuesInRedis = buildFlowConfigParser().convert(value);
Assert.assertEquals(flowRulesValuesInRedis.size(), 1);
Assert.assertEquals(flowRulesValuesInRedis.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
}
示例8
/**
* Get PubSub connection
* @return connection instance
*/
private StatefulRedisPubSubConnection pubsub() {
if(redisClient instanceof RedisClient)
return ((RedisClient)redisClient).connectPubSub();
else if(redisClient instanceof RedisClusterClient)
return ((RedisClusterClient)redisClient).connectPubSub();
return null;
}
示例9
@Override
public void publish(Command cmd) {
cmd.setSrc(LOCAL_COMMAND_ID);
try (StatefulRedisPubSubConnection<String, String> connection = this.pubsub()){
RedisPubSubCommands<String, String> sync = connection.sync();
sync.publish(this.channel, cmd.json());
}
}
示例10
private void subscribeFromChannel(String channel) {
StatefulRedisPubSubConnection<String, String> pubSubConnection = redisClient.connectPubSub();
RedisPubSubAdapter<String, String> adapterListener = new DelegatingRedisPubSubListener();
pubSubConnection.addListener(adapterListener);
RedisPubSubCommands<String, String> sync = pubSubConnection.sync();
sync.subscribe(channel);
}
示例11
@Test
public void testPubMsgAndReceiveSuccess() {
List<FlowRule> rules = FlowRuleManager.getRules();
Assert.assertEquals(1, rules.size());
int maxQueueingTimeMs = new Random().nextInt();
StatefulRedisPubSubConnection<String, String> connection = client.connectPubSub();
String flowRules =
"[{\"resource\":\"test\", \"limitApp\":\"default\", \"grade\":1, \"count\":\"0.0\", \"strategy\":0, "
+ "\"refResource\":null, "
+
"\"controlBehavior\":0, \"warmUpPeriodSec\":10, \"maxQueueingTimeMs\":" + maxQueueingTimeMs
+ ", \"controller\":null}]";
RedisPubSubCommands<String, String> subCommands = connection.sync();
subCommands.multi();
subCommands.set(ruleKey, flowRules);
subCommands.publish(channel, flowRules);
subCommands.exec();
await().timeout(2, TimeUnit.SECONDS)
.until(new Callable<List<FlowRule>>() {
@Override
public List<FlowRule> call() throws Exception {
return FlowRuleManager.getRules();
}
}, Matchers.hasSize(1));
rules = FlowRuleManager.getRules();
Assert.assertEquals(rules.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
String value = subCommands.get(ruleKey);
List<FlowRule> flowRulesValuesInRedis = buildFlowConfigParser().convert(value);
Assert.assertEquals(flowRulesValuesInRedis.size(), 1);
Assert.assertEquals(flowRulesValuesInRedis.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
}
示例12
@Override
public StatefulRedisPubSubConnection<K, V> getStatefulConnection() {
return new TracingStatefulRedisPubSubConnection<>(commands.getStatefulConnection(),
tracingConfiguration);
}
示例13
@Override
public StatefulRedisPubSubConnection<K, V> getStatefulConnection() {
return new TracingStatefulRedisPubSubConnection<>(commands.getStatefulConnection(),
tracingConfiguration);
}
示例14
public TracingStatefulRedisPubSubConnection(
StatefulRedisPubSubConnection<K, V> connection,
TracingConfiguration tracingConfiguration) {
super(connection, tracingConfiguration);
this.connection = connection;
}
示例15
@Override
public StatefulRedisPubSubConnection<K, V> getStatefulConnection() {
return new TracingStatefulRedisPubSubConnection<>(commands.getStatefulConnection(),
tracingConfiguration);
}
示例16
@Override
public StatefulRedisPubSubConnection<K, V> getStatefulConnection() {
return new TracingStatefulRedisPubSubConnection<>(commands.getStatefulConnection(),
tracingConfiguration);
}
示例17
public TracingStatefulRedisPubSubConnection(
StatefulRedisPubSubConnection<K, V> connection,
TracingConfiguration tracingConfiguration) {
super(connection, tracingConfiguration);
this.connection = connection;
}
示例18
@Override
public StatefulRedisPubSubConnection<K, V> getStatefulConnection() {
return new TracingStatefulRedisPubSubConnection<>(commands.getStatefulConnection(),
tracingConfiguration);
}
示例19
@Override
public StatefulRedisPubSubConnection<K, V> getStatefulConnection() {
return new TracingStatefulRedisPubSubConnection<>(commands.getStatefulConnection(),
tracingConfiguration);
}
示例20
public TracingStatefulRedisPubSubConnection(
StatefulRedisPubSubConnection<K, V> connection,
TracingConfiguration tracingConfiguration) {
super(connection, tracingConfiguration);
this.connection = connection;
}