Skip to content

Commit 8145935

Browse files
committed
Revert "remove the spring-worker-example until eclipse-vertx/vertx-codegen#131 is fixed"
This reverts commit 8ff2cc1.
1 parent 8ff2cc1 commit 8145935

File tree

15 files changed

+779
-1
lines changed

15 files changed

+779
-1
lines changed

spring-examples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<module>spring-example</module>
1616
<module>springboot-clustering</module>
1717
<module>spring-verticle-factory</module>
18-
<!--<module>spring-worker-example</module>-->
18+
<module>spring-worker-example</module>
1919
</modules>
2020

2121
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
= Vert.x Spring Worker example
2+
3+
This example shows how you can embed Vert.x in Spring Boot, having:
4+
5+
- Spring container creating the verticles
6+
- a standard verticle with Vert.x web for the REST API
7+
- Spring backend services (with declarative transaction management and JPA repositories)
8+
- a worker verticle exposing the Spring services over the event bus through service proxies
9+
10+
This might be useful if you have an existing Spring codebase and want to keep your data access layer.
11+
12+
To try the example, execute this command in a terminal:
13+
14+
[source,shell]
15+
----
16+
mvn clean spring-boot:run
17+
----
18+
19+
Then add a book to the database:
20+
21+
[source,shell]
22+
----
23+
curl http://localhost:8080/book --data '{"name": "The Practice of System and Network Administration", "author":"Thomas A. Limoncelli, Christina J. Hn, Strata R. Chalup"}'
24+
----
25+
26+
You should now be able to see your book saved:
27+
28+
[source,shell]
29+
----
30+
curl http://localhost:8080/books
31+
----
32+
33+
Notice that the `id` field is now set which means that the book as indeed been persisted in the database.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>io.vertx</groupId>
9+
<artifactId>spring-examples</artifactId>
10+
<version>3.4.2</version>
11+
</parent>
12+
13+
<artifactId>spring-worker-example</artifactId>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
18+
<java.version>1.8</java.version>
19+
<boot.version>1.4.3.RELEASE</boot.version>
20+
<h2.version>1.4.191</h2.version>
21+
</properties>
22+
23+
<dependencyManagement>
24+
<dependencies>
25+
<dependency>
26+
<!-- Import dependency management from Spring Boot -->
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-dependencies</artifactId>
29+
<version>${boot.version}</version>
30+
<type>pom</type>
31+
<scope>import</scope>
32+
</dependency>
33+
</dependencies>
34+
</dependencyManagement>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-data-jpa</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>com.h2database</groupId>
43+
<artifactId>h2</artifactId>
44+
<version>${h2.version}</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>io.vertx</groupId>
48+
<artifactId>vertx-web</artifactId>
49+
<version>${project.version}</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>io.vertx</groupId>
53+
<artifactId>vertx-service-proxy</artifactId>
54+
<version>${project.version}</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>io.vertx</groupId>
58+
<artifactId>vertx-codegen</artifactId>
59+
<version>${project.version}</version>
60+
<scope>provided</scope>
61+
</dependency>
62+
</dependencies>
63+
64+
<build>
65+
<pluginManagement>
66+
<plugins>
67+
<!-- We specify the Maven compiler plugin as we need to set it to Java 1.8 -->
68+
<plugin>
69+
<artifactId>maven-compiler-plugin</artifactId>
70+
<version>3.1</version>
71+
<configuration>
72+
<source>${java.version}</source>
73+
<target>${java.version}</target>
74+
<annotationProcessors>
75+
<annotationProcessor>io.vertx.codegen.CodeGenProcessor</annotationProcessor>
76+
</annotationProcessors>
77+
<compilerArgs>
78+
<arg>-AoutputDirectory=${project.basedir}/src/main</arg>
79+
</compilerArgs>
80+
</configuration>
81+
</plugin>
82+
</plugins>
83+
</pluginManagement>
84+
<plugins>
85+
<plugin>
86+
<groupId>org.springframework.boot</groupId>
87+
<artifactId>spring-boot-maven-plugin</artifactId>
88+
<version>${boot.version}</version>
89+
</plugin>
90+
</plugins>
91+
</build>
92+
93+
<profiles>
94+
<profile>
95+
<id>staging</id>
96+
<repositories>
97+
<repository>
98+
<id>staging</id>
99+
<url>https://oss.sonatype.org/content/repositories/iovertx-3295</url>
100+
</repository>
101+
</repositories>
102+
</profile>
103+
</profiles>
104+
105+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
= Cheatsheets
2+
3+
[[Book]]
4+
== Book
5+
6+
++++
7+
A book JPA entity.
8+
++++
9+
'''
10+
11+
[cols=">25%,^25%,50%"]
12+
[frame="topbot"]
13+
|===
14+
^|Name | Type ^| Description
15+
|[[author]]`author`|`String`|-
16+
|[[id]]`id`|`Number (Long)`|-
17+
|[[name]]`name`|`String`|-
18+
|===
19+
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 2017 Red Hat, Inc.
3+
*
4+
* Red Hat licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package io.vertx.example.spring.worker;
18+
19+
import io.vertx.core.DeploymentOptions;
20+
import io.vertx.core.Vertx;
21+
import io.vertx.core.VertxOptions;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.beans.factory.annotation.Value;
26+
import org.springframework.boot.SpringApplication;
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
import org.springframework.boot.context.event.ApplicationReadyEvent;
29+
import org.springframework.context.event.EventListener;
30+
31+
import java.util.concurrent.CountDownLatch;
32+
import java.util.concurrent.atomic.AtomicBoolean;
33+
34+
import static java.util.concurrent.TimeUnit.*;
35+
36+
/**
37+
* @author Thomas Segismont
38+
*/
39+
@SpringBootApplication
40+
public class Application {
41+
private static final Logger LOG = LoggerFactory.getLogger(Application.class);
42+
43+
@Autowired
44+
SpringVerticleFactory verticleFactory;
45+
46+
/**
47+
* The Vert.x worker pool size, configured in the {@code application.properties} file.
48+
*
49+
* Make sure this is greater than {@link #springWorkerInstances}.
50+
*/
51+
@Value("${vertx.worker.pool.size}")
52+
int workerPoolSize;
53+
54+
/**
55+
* The number of {@link SpringWorker} instances to deploy, configured in the {@code application.properties} file.
56+
*/
57+
@Value("${vertx.springWorker.instances}")
58+
int springWorkerInstances;
59+
60+
public static void main(String[] args) {
61+
SpringApplication.run(Application.class, args);
62+
}
63+
64+
/**
65+
* Deploy verticles when the Spring application is ready.
66+
*/
67+
@EventListener
68+
public void deployVerticles(ApplicationReadyEvent event) {
69+
Vertx vertx = Vertx.vertx(new VertxOptions().setWorkerPoolSize(workerPoolSize));
70+
71+
// The verticle factory is registered manually because it is created by the Spring container
72+
vertx.registerVerticleFactory(verticleFactory);
73+
74+
CountDownLatch deployLatch = new CountDownLatch(2);
75+
AtomicBoolean failed = new AtomicBoolean(false);
76+
77+
String restApiVerticleName = verticleFactory.prefix() + ":" + BookRestApi.class.getName();
78+
vertx.deployVerticle(restApiVerticleName, ar -> {
79+
if (ar.failed()) {
80+
LOG.error("Failed to deploy verticle", ar.cause());
81+
failed.compareAndSet(false, true);
82+
}
83+
deployLatch.countDown();
84+
});
85+
86+
DeploymentOptions workerDeploymentOptions = new DeploymentOptions()
87+
.setWorker(true)
88+
// As worker verticles are never executed concurrently by Vert.x by more than one thread,
89+
// deploy multiple instances to avoid serializing requests.
90+
.setInstances(springWorkerInstances);
91+
String workerVerticleName = verticleFactory.prefix() + ":" + SpringWorker.class.getName();
92+
vertx.deployVerticle(workerVerticleName, workerDeploymentOptions, ar -> {
93+
if (ar.failed()) {
94+
LOG.error("Failed to deploy verticle", ar.cause());
95+
failed.compareAndSet(false, true);
96+
}
97+
deployLatch.countDown();
98+
});
99+
100+
try {
101+
if (!deployLatch.await(5, SECONDS)) {
102+
throw new RuntimeException("Timeout waiting for verticle deployments");
103+
} else if (failed.get()) {
104+
throw new RuntimeException("Failure while deploying verticles");
105+
}
106+
} catch (InterruptedException e) {
107+
throw new RuntimeException(e);
108+
}
109+
}
110+
111+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2017 Red Hat, Inc.
3+
*
4+
* Red Hat licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package io.vertx.example.spring.worker;
18+
19+
import io.vertx.codegen.annotations.DataObject;
20+
import io.vertx.core.json.JsonObject;
21+
22+
import javax.persistence.Entity;
23+
import javax.persistence.GeneratedValue;
24+
import javax.persistence.Id;
25+
26+
/**
27+
* A book JPA entity.
28+
*
29+
* @author Thomas Segismont
30+
*/
31+
@Entity
32+
// Book must annotated with @DataObject because it is used as a parameter type in BookAsyncService
33+
@DataObject(generateConverter = true)
34+
public class Book {
35+
36+
@Id
37+
@GeneratedValue
38+
private Long id;
39+
40+
private String name;
41+
42+
private String author;
43+
44+
// Mandatory for JPA entities
45+
protected Book() {
46+
}
47+
48+
public Book(String name, String author) {
49+
this.name = name;
50+
this.author = author;
51+
}
52+
53+
// Mandatory for data objects
54+
public Book(JsonObject jsonObject) {
55+
BookConverter.fromJson(jsonObject, this);
56+
}
57+
58+
public Long getId() {
59+
return id;
60+
}
61+
62+
public void setId(Long id) {
63+
this.id = id;
64+
}
65+
66+
public String getName() {
67+
return name;
68+
}
69+
70+
public void setName(String name) {
71+
this.name = name;
72+
}
73+
74+
public String getAuthor() {
75+
return author;
76+
}
77+
78+
public void setAuthor(String author) {
79+
this.author = author;
80+
}
81+
82+
public JsonObject toJson() {
83+
JsonObject json = new JsonObject();
84+
BookConverter.toJson(this, json);
85+
return json;
86+
}
87+
88+
@Override
89+
public String toString() {
90+
return "Book{" +
91+
"id=" + id +
92+
", name='" + name + '\'' +
93+
", author='" + author + '\'' +
94+
'}';
95+
}
96+
}

0 commit comments

Comments
 (0)