Add more interesting messaging demo

This commit is contained in:
Dave Syer
2015-08-12 14:45:22 +01:00
parent 673d8cb0b8
commit 47dee39bb3
7 changed files with 138 additions and 7 deletions

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample;
import java.util.Random;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Trace;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
* @author Spencer Gibb
*/
@Component
public class SampleBackground {
@Autowired
private Trace trace;
@SneakyThrows
@Async
public void background() {
final Random random = new Random();
int millis = random.nextInt(1000);
Thread.sleep(millis);
this.trace.addKVAnnotation("background-sleep-millis", String.valueOf(millis));
}
}

View File

@@ -43,7 +43,10 @@ import com.github.kristofa.brave.SpanCollector;
public class SampleMessagingApplication {
@Autowired
private SampleGateway gateway;
private SampleSink gateway;
@Autowired
private SampleRequestResponse transformer;
@Bean
public Sampler<?> defaultSampler() {
@@ -57,6 +60,12 @@ public class SampleMessagingApplication {
return msg;
}
@RequestMapping("/xform")
public String xform() {
String msg = "Hello";
return this.transformer.send(msg);
}
public static void main(String[] args) {
SpringApplication.run(SampleMessagingApplication.class, args);
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
/**
* @author Dave Syer
*
*/
@MessagingGateway
public interface SampleRequestResponse {
@Gateway(requestChannel="xform")
String send(String input);
}

View File

@@ -24,7 +24,7 @@ import org.springframework.integration.annotation.MessagingGateway;
*
*/
@MessagingGateway
public interface SampleGateway {
public interface SampleSink {
@Gateway(requestChannel="messages")
void send(String message);

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
/**
* @author Dave Syer
*
*/
@MessageEndpoint
@Slf4j
public class SampleTransformer {
@Autowired
SampleBackground background;
@ServiceActivator(inputChannel="xform")
public String log(Message<?> message) {
log.info("Received: " + message);
this.background.background();
return message.getPayload().toString().toUpperCase();
}
}