Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring Integration channel adapter code samples #755

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<module>pubsub/cloud-client</module>
<module>spanner/cloud-client</module>
<module>speech/cloud-client</module>
<module>spring/integration/pubsub</module>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets just use spring/pubsub for now

<module>storage/cloud-client</module>
<module>storage/json-api</module>
<module>storage/storage-transfer</module>
Expand Down
120 changes: 120 additions & 0 deletions spring/integration/pubsub/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="UTF-8"?>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the files are missing the license header.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why XML configs should be with the License as well?
Is that your policy?
We in Spring only care about classes (Java/Groovy/Kothlin etc.)

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>doc-samples</artifactId>
<groupId>com.google.cloud</groupId>
<version>1.0.0</version>
<relativePath>../../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>

<artifactId>spring-integration-pubsub-samples</artifactId>

<properties>
<spring-cloud-gcp.version>1.0.0.BUILD-SNAPSHOT</spring-cloud-gcp.version>
<spring-boot.version>1.5.2.RELEASE</spring-boot.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-pubsub</artifactId>
<version>${spring-cloud-gcp.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-integration-gcp</artifactId>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I think it's a transitive dependency of spring-cloud-gcp-starter-pubsub.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one isn't.

<version>${spring-cloud-gcp.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
</dependencies>

<!-- Angular -->

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>${project.build.directory}/generated-resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.2.RELEASE</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<!-- Serves *only* to filter the wro.xml so it can get an absolute
path for the project -->
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/wro</outputDirectory>
<resources>
<resource>
<directory>src/main/wro</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>ro.isdc.wro4j</groupId>
<artifactId>wro4j-maven-plugin</artifactId>
<version>1.7.6</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
<wroManagerFactory>ro.isdc.wro.maven.plugin.manager.factory.ConfigurableWroManagerFactory</wroManagerFactory>
<cssDestinationFolder>${project.build.directory}/generated-resources/static/css</cssDestinationFolder>
<jsDestinationFolder>${project.build.directory}/generated-resources/static/js</jsDestinationFolder>
<wroFile>${project.build.directory}/wro/wro.xml</wroFile>
<extraConfigFile>src/main/wro/wro.properties</extraConfigFile>
<contextFolder>${basedir}/src/main/wro</contextFolder>
</configuration>
<dependencies>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>angularjs</artifactId>
<version>1.3.8</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.example.spring.pubsub;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

License header missing


import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import com.google.cloud.pubsub.v1.AckReplyConsumer;
import com.google.protobuf.ByteString;
import com.google.pubsub.v1.Subscription;
import com.google.pubsub.v1.SubscriptionName;
import com.google.pubsub.v1.Topic;
import com.google.pubsub.v1.TopicName;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gcp.pubsub.PubsubAdmin;
import org.springframework.cloud.gcp.pubsub.core.PubsubTemplate;
import org.springframework.cloud.gcp.pubsub.support.GcpHeaders;
import org.springframework.cloud.gcp.pubsub.support.SubscriberFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.PublishSubscribeChannel;
import org.springframework.integration.gcp.AckMode;
import org.springframework.integration.gcp.inbound.PubsubInboundChannelAdapter;
import org.springframework.integration.gcp.outbound.PubsubMessageHandler;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;

@SpringBootApplication
@RestController
public class PubsubApplication {

private static final Log LOGGER = LogFactory.getLog(PubsubApplication.class);

@Autowired
private PubsubOutboundGateway messagingGateway;

@Autowired
private PubsubAdmin admin;

public static void main(String[] args) throws IOException {
SpringApplication.run(PubsubApplication.class, args);
}

@GetMapping("/listTopics")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth moving the @RestController and mappings to a different class and keeping the Application class simple (less cognitive load)

public List<String> listTopics() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public methods : please provide a Javadoc

return admin.listTopics().stream()
.map(Topic::getNameAsTopicName)
.map(TopicName::getTopic)
.collect(Collectors.toList());
}

@GetMapping("/listSubscriptions")
public List<String> listSubscriptions() {
return admin.listSubscriptions().stream()
.map(Subscription::getNameAsSubscriptionName)
.map(SubscriptionName::getSubscription)
.collect(Collectors.toList());
}

@PostMapping("/postMessage")
public RedirectView addMessage(@RequestParam("message") String message) {
messagingGateway.sendToPubsub(message);
return new RedirectView("/");
}

@PostMapping("/newTopic")
public RedirectView newTopic(@RequestParam("name") String topicName) {
admin.createTopic(topicName);
return new RedirectView("/");
}

@PostMapping("/newSubscription")
public RedirectView newSubscription(@RequestParam("name") String subscriptionName,
@RequestParam("topic") String topicName) {
admin.createSubscription(subscriptionName, topicName);
return new RedirectView("/");
}

@Bean
public MessageChannel pubsubInputChannel() {
return new PublishSubscribeChannel();
}

@Bean
public PubsubInboundChannelAdapter messageChannelAdapter(
@Qualifier("pubsubInputChannel") MessageChannel inputChannel,
SubscriberFactory subscriberFactory) {
PubsubInboundChannelAdapter adapter =
new PubsubInboundChannelAdapter(subscriberFactory, "messages");
adapter.setOutputChannel(inputChannel);
adapter.setAckMode(AckMode.MANUAL);

return adapter;
}

@Bean
@ServiceActivator(inputChannel = "pubsubInputChannel")
public MessageHandler messageReceiver1() {
return message -> {
LOGGER.info("Message arrived! Payload: "
+ ((ByteString) message.getPayload()).toStringUtf8());
AckReplyConsumer consumer = (AckReplyConsumer) message.getHeaders().get(
GcpHeaders.ACKNOWLEDGEMENT);
consumer.ack();
};
}

@Bean
@ServiceActivator(inputChannel = "pubsubInputChannel")
public MessageHandler messageReceiver2() {
return message -> {
LOGGER.info("Message also arrived here! Payload: "
+ ((ByteString) message.getPayload()).toStringUtf8());
AckReplyConsumer consumer = (AckReplyConsumer) message.getHeaders().get(
GcpHeaders.ACKNOWLEDGEMENT);
consumer.ack();
};
}

@Bean
@ServiceActivator(inputChannel = "pubsubOutputChannel")
public MessageHandler messageSender(PubsubTemplate pubsubTemplate) {
PubsubMessageHandler outboundAdapter = new PubsubMessageHandler(pubsubTemplate);
outboundAdapter.setTopic("test");
Copy link
Member

@meltsufin meltsufin Jul 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who creates this "test" topic?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to have been previously created.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better to provide the ability to select a topic to which to post the message then?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't do that dynamically.

return outboundAdapter;
}

@MessagingGateway(defaultRequestChannel = "pubsubOutputChannel")
public interface PubsubOutboundGateway {

void sendToPubsub(String text);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#spring.cloud.gcp.projectId=[YOUR_PROJECT_ID]
#spring.cloud.gcp.credentialsLocation=file:[LOCAL_PATH_TO_CREDENTIALS]
#
#spring.cloud.gcp.pubsub.subscriber.executorThreads=[SUBSCRIBER_THREADS]
#spring.cloud.gcp.pubsub.publisher.executorThreads=[PUBLISHER_THREADS]
36 changes: 36 additions & 0 deletions spring/integration/pubsub/src/main/resources/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Spring Integration GCP sample</title>
<link href="css/angular-bootstrap.css" rel="stylesheet">
<script src="js/angular-bootstrap.js" type="text/javascript"></script>
<script src="js/pubsub.js"></script>
</head>
<body ng-app="pubsub">
<div name="formDiv">
<form action="/postMessage" method="post">
Post message: <input type="text" name="message" /> <input type="submit" />
</form>
<form action="/newTopic" method="post">
New topic: <input type="text" name="name" /> <input type="submit" />
</form>
<form action="/newSubscription" method="post">
New subscription: <input type="text" name="name" />
for topic <input type="text" name="topic" /> <input type="submit" />
</form>
</div>
<div ng-controller="listTopics">
<h1>Topics</h1>
<div ng-repeat="topic in topics">
{{ topic }}<br/>
</div>
</div>
<div ng-controller="listSubscriptions">
<h1>Subscriptions</h1>
<div ng-repeat="subscription in subscriptions">
{{ subscription }}<br/>
</div>
</div>
</body>
</html>
11 changes: 11 additions & 0 deletions spring/integration/pubsub/src/main/resources/static/js/pubsub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
angular.module('pubsub', [])
.controller('listTopics', function($scope, $http) {
$http.get('/listTopics').success(function(data) {
$scope.topics = data;
});
})
.controller('listSubscriptions', function($scope, $http) {
$http.get('/listSubscriptions').success(function(data) {
$scope.subscriptions = data;
})
});
Empty file.
4 changes: 4 additions & 0 deletions spring/integration/pubsub/src/main/wro/wro.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#List of preProcessors
preProcessors=lessCssImport
#List of postProcessors
postProcessors=less4j,jsMin
9 changes: 9 additions & 0 deletions spring/integration/pubsub/src/main/wro/wro.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<groups xmlns="http://www.isdc.ro/wro">
<group name="angular-bootstrap">
<css>webjar:bootstrap/3.2.0/less/bootstrap.less</css>
<css>file:${project.basedir}/src/main/wro/main.less</css>
<js>webjar:jquery/2.1.1/jquery.min.js</js>
<js>webjar:bootstrap/3.2.0/bootstrap.js</js>
<js>webjar:angularjs/1.3.8/angular.min.js</js>
</group>
</groups>