下面列出了org.springframework.boot.actuate.endpoint.annotation.WriteOperation#org.springframework.cloud.bus.event.RefreshRemoteApplicationEvent 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
void trigger_refresh() throws Exception {
assertThatConfiguredValueIs("admin");
configServerValues.put(ConfigClient.USER_ROLE_KEY, "dev");
assertThatConfiguredValueIs("admin");
contextRefreshLock.reset();
RefreshRemoteApplicationEvent event = new RefreshRemoteApplicationEvent(this, "test-id", null);
logger.info("\n\n <<<refresh event sent through RabbitMQ>>>\n\n");
rabbitTemplate.convertAndSend("springCloudBus", "unused", event);
contextRefreshLock.acquire();
assertThatConfiguredValueIs("dev");
}
@Test
public void inboundNotFromSelfWithAck() throws Exception {
this.context = SpringApplication.run(
new Class[] { InboundMessageHandlerConfiguration.class,
OutboundMessageHandlerConfiguration.class,
SentMessageConfiguration.class },
new String[] { "--spring.cloud.bus.id=bar", "--server.port=0" });
this.context.getBean(SpringCloudBusClient.INPUT, MessageChannel.class)
.send(new GenericMessage<>(
new RefreshRemoteApplicationEvent(this, "foo", null)));
RefreshRemoteApplicationEvent refresh = this.context
.getBean(InboundMessageHandlerConfiguration.class).refresh;
assertThat(refresh).isNotNull();
OutboundMessageHandlerConfiguration outbound = this.context
.getBean(OutboundMessageHandlerConfiguration.class);
outbound.latch.await(2000L, TimeUnit.MILLISECONDS);
String message = (String) outbound.message.getPayload();
assertThat(message.contains("\"ackId\":\"" + refresh.getId()))
.as("Wrong ackId: " + message).isTrue();
}
@Test
public void inboundNotFromSelfWithTrace() throws Exception {
this.context = SpringApplication.run(
new Class[] { InboundMessageHandlerConfiguration.class,
OutboundMessageHandlerConfiguration.class,
SentMessageConfiguration.class },
new String[] { "--spring.cloud.bus.trace.enabled=true",
"--spring.cloud.bus.id=bar", "--server.port=0" });
this.context.getBean(SpringCloudBusClient.INPUT, MessageChannel.class)
.send(new GenericMessage<>(
new RefreshRemoteApplicationEvent(this, "foo", null)));
RefreshRemoteApplicationEvent refresh = this.context
.getBean(InboundMessageHandlerConfiguration.class).refresh;
assertThat(refresh).isNotNull();
SentMessageConfiguration sent = this.context
.getBean(SentMessageConfiguration.class);
assertThat(sent.event).isNotNull();
assertThat(sent.count).isEqualTo(1);
}
@Test
public void inboundAckWithTrace() throws Exception {
this.context = SpringApplication.run(
new Class[] { InboundMessageHandlerConfiguration.class,
OutboundMessageHandlerConfiguration.class,
AckMessageConfiguration.class },
new String[] { "--spring.cloud.bus.trace.enabled=true",
"--spring.cloud.bus.id=bar", "--server.port=0" });
this.context.getBean(BusProperties.class).setId("bar");
this.context.getBean(SpringCloudBusClient.INPUT, MessageChannel.class)
.send(new GenericMessage<>(new AckRemoteApplicationEvent(this, "foo",
null, "ID", "bar", RefreshRemoteApplicationEvent.class)));
AckMessageConfiguration sent = this.context
.getBean(AckMessageConfiguration.class);
assertThat(sent.event).isNotNull();
assertThat(sent.count).isEqualTo(1);
}
@RequestMapping(method = RequestMethod.POST)
public Set<String> notifyByPath(@RequestHeader HttpHeaders headers,
@RequestBody Map<String, Object> request) {
PropertyPathNotification notification = this.extractor.extract(headers, request);
if (notification != null) {
Set<String> services = new LinkedHashSet<>();
for (String path : notification.getPaths()) {
services.addAll(guessServiceName(path));
}
if (this.applicationEventPublisher != null) {
for (String service : services) {
log.info("Refresh for: " + service);
this.applicationEventPublisher.publishEvent(
new RefreshRemoteApplicationEvent(this, this.busId, service));
}
return services;
}
}
return Collections.emptySet();
}
@Test
public void business() throws Exception {
Message<RefreshRemoteApplicationEvent> message = MessageBuilder
.withPayload(new RefreshRemoteApplicationEvent(this, "me", "you")).build();
this.bus.springCloudBusOutput().send(message);
String payload = (String) this.collector.forChannel(this.bus.springCloudBusOutput()).take().getPayload();
assertTrue("Wrong payload: " + payload, payload.contains("\"type\""));
}
@Test
public void vanillaDeserialize() throws Exception {
this.mapper.registerModule(new SubtypeModule(RefreshRemoteApplicationEvent.class,
EnvironmentChangeRemoteApplicationEvent.class));
EnvironmentChangeRemoteApplicationEvent source = new EnvironmentChangeRemoteApplicationEvent(
this, "foo", "bar", Collections.<String, String>emptyMap());
String value = this.mapper.writeValueAsString(source);
RemoteApplicationEvent event = this.mapper.readValue(value,
RemoteApplicationEvent.class);
assertThat(event instanceof EnvironmentChangeRemoteApplicationEvent).isTrue();
assertThat(event.getId()).isNotNull();
assertThat(event.getId().equals(source.getId())).isTrue();
}
@Test
public void deserializeOldValueWithNoId() throws Exception {
this.mapper.registerModule(new SubtypeModule(RefreshRemoteApplicationEvent.class,
EnvironmentChangeRemoteApplicationEvent.class));
EnvironmentChangeRemoteApplicationEvent source = new EnvironmentChangeRemoteApplicationEvent(
this, "foo", "bar", Collections.<String, String>emptyMap());
String value = this.mapper.writeValueAsString(source);
value = value.replaceAll(",\"id\":\"[a-f0-9-]*\"", "");
RemoteApplicationEvent event = this.mapper.readValue(value,
RemoteApplicationEvent.class);
assertThat(event instanceof EnvironmentChangeRemoteApplicationEvent).isTrue();
assertThat(event.getId()).isNotNull();
assertThat(event.getId().equals(source.getId())).isFalse();
}
private void addStandardSpringCloudEventBusEvents(
final List<Class<?>> expectedRegisterdClassesAsList) {
expectedRegisterdClassesAsList.add(AckRemoteApplicationEvent.class);
expectedRegisterdClassesAsList.add(EnvironmentChangeRemoteApplicationEvent.class);
expectedRegisterdClassesAsList.add(RefreshRemoteApplicationEvent.class);
expectedRegisterdClassesAsList.add(UnknownRemoteApplicationEvent.class);
}
@Test
public void inboundNotForSelf() {
this.context = SpringApplication.run(InboundMessageHandlerConfiguration.class,
"--spring.cloud.bus.id=foo", "--server.port=0");
this.context.getBean(SpringCloudBusClient.INPUT, MessageChannel.class)
.send(new GenericMessage<>(
new RefreshRemoteApplicationEvent(this, "bar", "bar")));
assertThat(this.context.getBean(InboundMessageHandlerConfiguration.class).refresh)
.isNull();
}
@Test
public void inboundFromSelf() {
this.context = SpringApplication.run(InboundMessageHandlerConfiguration.class,
"--spring.cloud.bus.id=foo", "--server.port=0");
this.context.getBean(SpringCloudBusClient.INPUT, MessageChannel.class)
.send(new GenericMessage<>(
new RefreshRemoteApplicationEvent(this, "foo", null)));
assertThat(this.context.getBean(InboundMessageHandlerConfiguration.class).refresh)
.isNull();
}
@Test
public void inboundNotFromSelf() {
this.context = SpringApplication.run(InboundMessageHandlerConfiguration.class,
"--spring.cloud.bus.id=bar", "--server.port=0");
this.context.getBean(SpringCloudBusClient.INPUT, MessageChannel.class)
.send(new GenericMessage<>(
new RefreshRemoteApplicationEvent(this, "foo", null)));
assertThat(this.context.getBean(InboundMessageHandlerConfiguration.class).refresh)
.isNotNull();
}
@Test
public void outboundFromSelf() throws Exception {
this.context = SpringApplication.run(OutboundMessageHandlerConfiguration.class,
"--debug=true", "--spring.cloud.bus.id=foo", "--server.port=0");
this.context.publishEvent(new RefreshRemoteApplicationEvent(this, "foo", null));
OutboundMessageHandlerConfiguration outbound = this.context
.getBean(OutboundMessageHandlerConfiguration.class);
outbound.latch.await(2000L, TimeUnit.MILLISECONDS);
assertThat(outbound.message).as("message was null").isNotNull();
}
@Test
public void outboundNotFromSelf() {
this.context = SpringApplication.run(OutboundMessageHandlerConfiguration.class,
"--spring.cloud.bus.id=bar", "--server.port=0");
this.context.publishEvent(new RefreshRemoteApplicationEvent(this, "foo", null));
assertThat(
this.context.getBean(OutboundMessageHandlerConfiguration.class).message)
.isNull();
}
@Test
public void inboundNotFromSelfPathPattern() {
this.context = SpringApplication.run(InboundMessageHandlerConfiguration.class,
"--spring.cloud.bus.id=bar:1000", "--server.port=0");
this.context.getBean(SpringCloudBusClient.INPUT, MessageChannel.class)
.send(new GenericMessage<>(
new RefreshRemoteApplicationEvent(this, "foo", "bar:*")));
assertThat(this.context.getBean(InboundMessageHandlerConfiguration.class).refresh)
.isNotNull();
}
@Test
public void inboundNotFromSelfDeepPathPattern() {
this.context = SpringApplication.run(InboundMessageHandlerConfiguration.class,
"--spring.cloud.bus.id=bar:test:1000", "--server.port=0");
this.context.getBean(SpringCloudBusClient.INPUT, MessageChannel.class)
.send(new GenericMessage<>(
new RefreshRemoteApplicationEvent(this, "foo", "bar:**")));
assertThat(this.context.getBean(InboundMessageHandlerConfiguration.class).refresh)
.isNotNull();
}
@Test
public void inboundNotFromSelfFlatPattern() {
this.context = SpringApplication.run(InboundMessageHandlerConfiguration.class,
"--spring.cloud.bus.id=bar", "--server.port=0");
this.context.getBean(SpringCloudBusClient.INPUT, MessageChannel.class)
.send(new GenericMessage<>(
new RefreshRemoteApplicationEvent(this, "foo", "bar*")));
assertThat(this.context.getBean(InboundMessageHandlerConfiguration.class).refresh)
.isNotNull();
}
@WriteOperation
public void busRefreshWithDestination(@Selector String destination) {
publish(new RefreshRemoteApplicationEvent(this, getInstanceId(), destination));
}
@WriteOperation
public void busRefresh() {
publish(new RefreshRemoteApplicationEvent(this, getInstanceId(), null));
}
@Override
public void onApplicationEvent(RefreshRemoteApplicationEvent event) {
this.refresh = event;
}