Skip to content

Commit c719f8b

Browse files
committed
Updated getting started docs to use Spring Initializer
resolves spring-cloud#423
1 parent cba26ed commit c719f8b

File tree

1 file changed

+51
-130
lines changed

1 file changed

+51
-130
lines changed

spring-cloud-task-docs/src/main/asciidoc/getting-started.adoc

Lines changed: 51 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -52,134 +52,45 @@ http://start.spring.io/[Spring Initializr] and creating a new project. Doing so
5252
automatically generates a new project structure so that you can start coding right away.
5353
We recommend experimenting with the Spring Initializr to become familiar with it.
5454

55-
Before we begin, open a terminal to check that you have valid versions of Java and Maven
56-
installed, as shown in the following two listings:
55+
[[getting-started-creating-project]]
56+
=== Creating the Spring Task Project using Spring Initializr
57+
Now we can create and test an application that prints `Hello, World!` to the console.
5758

58-
[source]
59-
$ java -version
60-
java version "1.8.0_31"
61-
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
62-
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
63-
64-
[source]
65-
$ mvn -v
66-
Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T15:58:10-05:00)
67-
Maven home: /usr/local/Cellar/maven/3.2.3/libexec
68-
Java version: 1.8.0_31, vendor: Oracle Corporation
69-
70-
NOTE: This sample needs to be created in its own folder. Subsequent instructions assume
71-
you have created a suitable folder and that it is your "`current directory.`"
72-
73-
[[getting-started-creating-the-pom]]
74-
=== Creating the POM
75-
76-
We need to start by creating a Maven `pom.xml` file. The `pom.xml` file contains the
77-
recipe that Maven uses to build your project. To create the pom.xml file, open your
78-
favorite text editor and add the following:
79-
80-
[code,xml]
81-
----
82-
<?xml version="1.0" encoding="UTF-8"?>
83-
<project xmlns="http://maven.apache.org/POM/4.0.0"
84-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
85-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
86-
87-
<modelVersion>4.0.0</modelVersion>
88-
89-
<groupId>com.example</groupId>
90-
<artifactId>myproject</artifactId>
91-
<packaging>jar</packaging>
92-
<version>0.0.1-SNAPSHOT</version>
93-
94-
<parent>
95-
<groupId>org.springframework.boot</groupId>
96-
<artifactId>spring-boot-starter-parent</artifactId>
97-
<version>1.5.2.RELEASE</version>
98-
</parent>
99-
100-
<properties>
101-
<start-class>com.example.SampleTask</start-class>
102-
</properties>
103-
104-
<dependencies>
105-
<dependency>
106-
<groupId>org.springframework.boot</groupId>
107-
<artifactId>spring-boot-starter</artifactId>
108-
</dependency>
109-
</dependencies>
110-
111-
<build>
112-
<plugins>
113-
<plugin>
114-
<groupId>org.springframework.boot</groupId>
115-
<artifactId>spring-boot-maven-plugin</artifactId>
116-
</plugin>
117-
</plugins>
118-
</build>
119-
</project>
120-
----
121-
122-
Creating a `pom.xml` file with the preceding content should give you a working build. You
123-
can test it by running `mvn package` (for now, you can ignore the "jar will be empty - no
124-
content was marked for inclusion!" warning ).
125-
126-
NOTE: At this point, you could import the project into an IDE (most modern Java IDE's
127-
include built-in support for Maven). For simplicity, we continue to use a plain text
128-
editor for this example.
59+
To do so:
12960

130-
[[getting-started-adding-classpath-dependencies]]
131-
=== Adding classpath dependencies
132-
133-
A Spring Cloud Task is made up of a Spring Boot application that is expected to end. In
134-
the `pom.xml` file we showed earlier, we created the shell of a Spring Boot application by
135-
setting our parent to use the `spring-boot-starter-parent`.
136-
137-
Spring Boot provides a number of additional "`Starter POMs`". Some of them are appropriate
138-
for use within tasks (`spring-boot-starter-batch`, `spring-boot-starter-jdbc`, and
139-
others), and some may not be ('spring-boot-starter-web` is probably not going to be used
140-
in a task). The best indicator of which starter makes sense is whether the resulting
141-
application should end. Batch-based applications typically end. Conversely, the
142-
`spring-boot-starter-web` dependency bootstraps a servlet container, which better suits
143-
applications that continue.
144-
145-
For this example, we need only to add a single additional dependency -- the one for
146-
Spring Cloud Task itself:
147-
148-
[source,xml]
149-
<dependency>
150-
<groupId>org.springframework.cloud</groupId>
151-
<artifactId>spring-cloud-starter-task</artifactId>
152-
<version>1.2.3.RELEASE</version>
153-
</dependency>
61+
. Visit the link:https://start.spring.io/[Spring Initialzr] site.
62+
.. Create a new Maven project with a *Group* name of `io.spring.demo` and an *Artifact* name of `helloworld`.
63+
.. In the Dependencies text box, type `task` and then select the `Cloud Task` dependency.
64+
.. In the Dependencies text box, type `jdbc` and then select the `JDBC` dependency.
65+
.. In the Dependencies text box, type `h2` and then select the `H2`. (or your favorite database)
66+
.. Click the *Generate Project* button
67+
. Unzip the timestamp.zip file and import the project into your favorite IDE.
15468

15569
[[getting-started-writing-the-code]]
15670
=== Writing the Code
15771

158-
To finish our application, we need to create a single Java file. By default, Maven
159-
compiles the sources from `src/main/java`, so you need to create that folder structure.
160-
Then you need to add a file named `src/main/java/com/example/SampleTask.java`, as shown
161-
in the following example:
162-
72+
To finish our application, we need to update the generated `HelloworldApplication` with the following contents so that it launches a Task.
16373
[source,java]
16474
----
165-
package com.example;
75+
package io.spring.demo.helloworld;
16676
167-
import org.springframework.boot.*;
77+
import org.springframework.boot.CommandLineRunner;
78+
import org.springframework.boot.SpringApplication;
16879
import org.springframework.boot.autoconfigure.SpringBootApplication;
16980
import org.springframework.cloud.task.configuration.EnableTask;
17081
import org.springframework.context.annotation.Bean;
17182
17283
@SpringBootApplication
17384
@EnableTask
174-
public class SampleTask {
85+
public class HelloworldApplication {
17586
17687
@Bean
17788
public CommandLineRunner commandLineRunner() {
17889
return new HelloWorldCommandLineRunner();
17990
}
18091
18192
public static void main(String[] args) {
182-
SpringApplication.run(SampleTask.class, args);
93+
SpringApplication.run(HelloworldApplication.class, args);
18394
}
18495
18596
public static class HelloWorldCommandLineRunner implements CommandLineRunner {
@@ -196,11 +107,15 @@ While it may seem small, quite a bit is going on. For more about Spring
196107
Boot specifics, see the
197108
http://docs.spring.io/spring-boot/docs/current/reference/html/[Spring Boot reference documentation].
198109

199-
We also need to create an `application.properties` file in `src/main/resources`. We need
200-
to configure two properties in `application.properties`: We need to set the application
201-
name (which is translated to the task name), and we need to set the logging for Spring
202-
Cloud Task to `DEBUG` so that we can see what's going on. The following example shows how
203-
to do both:
110+
Now we can open the `application.properties` file in `src/main/resources`.
111+
We need to configure two properties in `application.properties`:
112+
113+
* `application.name`: To set the application name (which is translated to the task name)
114+
* `logging.level`: To set the logging for Spring Cloud Task to `DEBUG` in order to
115+
get a view of what is going on.
116+
117+
The following example shows how to do both:
118+
204119

205120
[source]
206121
----
@@ -217,24 +132,23 @@ default, it imports an additional configuration class (`SimpleTaskConfiguration`
217132
additional configuration registers the `TaskRepository` and the infrastructure for its
218133
use.
219134

220-
Out of the box, the `TaskRepository` uses an in-memory `Map` to record the results
221-
of a task. A `Map` is not a practical solution for a production environment, since
222-
the `Map` goes away once the task ends. However, for a quick getting-started
223-
experience, we use this as a default as well as echoing to the logs what is being updated
135+
In our demo, the `TaskRepository` uses an embedded H2 database to record the results
136+
of a task. This H2 embedded database is not a practical solution for a production environment, since
137+
the H2 DB goes away once the task ends. However, for a quick getting-started
138+
experience, we can use this in our example as well as echoing to the logs what is being updated
224139
in that repository. In the <<features-configuration>> section (later in this
225140
documentation), we cover how to customize the configuration of the pieces provided by
226141
Spring Cloud Task.
227142

228143
When our sample application runs, Spring Boot launches our `HelloWorldCommandLineRunner`
229-
and outputs our "`Hello, World!`" message to standard out. The `TaskLifecyceListener`
144+
and outputs our "`Hello, World!`" message to standard out. The `TaskLifecycleListener`
230145
records the start of the task and the end of the task in the repository.
231146

232147
[[getting-started-main-method]]
233148
==== The main method
234149

235150
The main method serves as the entry point to any java application. Our main method
236-
delegates to Spring Boot's `SpringApplication` class. You can read more about it in the
237-
Spring Boot documentation.
151+
delegates to Spring Boot's https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-spring-application.html[SpringApplication] class.
238152

239153
[[getting-started-clr]]
240154
==== The CommandLineRunner
@@ -266,25 +180,32 @@ $ mvn clean spring-boot:run
266180
....... . . . (Maven log output here)
267181
....... . . .
268182
269-
270183
. ____ _ __ _ _
271184
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
272185
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
273186
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
274187
' |____| .__|_| |_|_| |_\__, | / / / /
275188
=========|_|==============|___/=/_/_/_/
276-
:: Spring Boot :: (v1.3.3.RELEASE)
277-
278-
2016-01-25 11:08:10.183 INFO 12943 --- [ main] com.example.SampleTask : Starting SampleTask on Michaels-MacBook-Pro-2.local with PID 12943 (/Users/mminella/Documents/IntelliJWorkspace/spring-cloud-task-example/target/classes started by mminella in /Users/mminella/Documents/IntelliJWorkspace/spring-cloud-task-example)
279-
2016-01-25 11:08:10.185 INFO 12943 --- [ main] com.example.SampleTask : No active profile set, falling back to default profiles: default
280-
2016-01-25 11:08:10.226 INFO 12943 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2a2c3676: startup date [Mon Jan 25 11:08:10 CST 2016]; root of context hierarchy
281-
2016-01-25 11:08:11.051 INFO 12943 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
282-
2016-01-25 11:08:11.065 INFO 12943 --- [ main] o.s.c.t.r.support.SimpleTaskRepository : Creating: TaskExecution{executionId=0, externalExecutionID='null', exitCode=0, taskName='application', startTime=Mon Jan 25 11:08:11 CST 2016, endTime=null, statusCode='null', exitMessage='null', arguments=[]}
189+
:: Spring Boot :: (v2.0.3.RELEASE)
190+
191+
2018-07-23 17:44:34.426 INFO 1978 --- [ main] i.s.d.helloworld.HelloworldApplication : Starting HelloworldApplication on Glenns-MBP-2.attlocal.net with PID 1978 (/Users/glennrenfro/project/helloworld/target/classes started by glennrenfro in /Users/glennrenfro/project/helloworld)
192+
2018-07-23 17:44:34.430 INFO 1978 --- [ main] i.s.d.helloworld.HelloworldApplication : No active profile set, falling back to default profiles: default
193+
2018-07-23 17:44:34.472 INFO 1978 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1d24f32d: startup date [Mon Jul 23 17:44:34 EDT 2018]; root of context hierarchy
194+
2018-07-23 17:44:35.280 INFO 1978 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
195+
2018-07-23 17:44:35.410 INFO 1978 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
196+
2018-07-23 17:44:35.419 DEBUG 1978 --- [ main] o.s.c.t.c.SimpleTaskConfiguration : Using org.springframework.cloud.task.configuration.DefaultTaskConfigurer TaskConfigurer
197+
2018-07-23 17:44:35.420 DEBUG 1978 --- [ main] o.s.c.t.c.DefaultTaskConfigurer : No EntityManager was found, using DataSourceTransactionManager
198+
2018-07-23 17:44:35.522 DEBUG 1978 --- [ main] o.s.c.t.r.s.TaskRepositoryInitializer : Initializing task schema for h2 database
199+
2018-07-23 17:44:35.525 INFO 1978 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from class path resource [org/springframework/cloud/task/schema-h2.sql]
200+
2018-07-23 17:44:35.558 INFO 1978 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from class path resource [org/springframework/cloud/task/schema-h2.sql] in 33 ms.
201+
2018-07-23 17:44:35.728 INFO 1978 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
202+
2018-07-23 17:44:35.730 INFO 1978 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure
203+
2018-07-23 17:44:35.733 INFO 1978 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
204+
2018-07-23 17:44:35.738 INFO 1978 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
205+
2018-07-23 17:44:35.762 DEBUG 1978 --- [ main] o.s.c.t.r.support.SimpleTaskRepository : Creating: TaskExecution{executionId=0, parentExecutionId=null, exitCode=null, taskName='application', startTime=Mon Jul 23 17:44:35 EDT 2018, endTime=null, exitMessage='null', externalExecutionId='null', errorMessage='null', arguments=[]}
206+
2018-07-23 17:44:35.772 INFO 1978 --- [ main] i.s.d.helloworld.HelloworldApplication : Started HelloworldApplication in 1.625 seconds (JVM running for 4.764)
283207
Hello, World!
284-
2016-01-25 11:08:11.071 INFO 12943 --- [ main] com.example.SampleTask : Started SampleTask in 1.095 seconds (JVM running for 3.826)
285-
2016-01-25 11:08:11.220 INFO 12943 --- [ Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2a2c3676: startup date [Mon Jan 25 11:08:10 CST 2016]; root of context hierarchy
286-
2016-01-25 11:08:11.222 INFO 12943 --- [ Thread-1] o.s.c.t.r.support.SimpleTaskRepository : Updating: TaskExecution{executionId=0, externalExecutionID='null', exitCode=0, taskName='application', startTime=Mon Jan 25 11:08:11 CST 2016, endTime=Mon Jan 25 11:08:11 CST 2016, statusCode='null', exitMessage='null', arguments=[]}
287-
2016-01-25 11:08:11.222 INFO 12943 --- [ Thread-1] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
208+
2018-07-23 17:44:35.782 DEBUG 1978 --- [ main] o.s.c.t.r.support.SimpleTaskRepository : Updating: TaskExecution with executionId=1 with the following {exitCode=0, endTime=Mon Jul 23 17:44:35 EDT 2018, exitMessage='null', errorMessage='null'}
288209
----
289210

290211
The preceding output has three lines that of interest to us here:

0 commit comments

Comments
 (0)