Skip to content

Commit 2c22345

Browse files
andrewdesevelinec
authored andcommitted
Multipane (#75)
* Adding multipane * Added linenums * Changing format of test explanation, fixing typo * Fix line numbers * Adding code_commands * Removing backticks, fixing hotspots * Adding hotspots to code_commands * Removed extra highlight lines * testing action to create files * Removing highlight * Minor fixess in spacing for multipane * Added inline hotspots and reverted previous code_commands * Updated multipane * Updated multipane * Updated multipane * Updated multipane * updated README * updated README * updated README * updated README * updated README * updated the POM files * updated POM file and README * updated README * updated README * updated README * updated README * update README * updated README * updated travis.yml * Move the code files to after where the hotspots refer to the file. For hotspots after the last code file, added a index of which file it is for * Fix hotspot ranges for the Handling dependencies in the application section * Fix add() hotspot * updated README * updated README * updated README * updated README * updated README * updated README * updated README and POM files * Moved includes to bottom of subsection * Moved includes to bottom of subsection * Revert * Updated hotspot * Reverted back to Rachel's original commit * updated README * updated files for multipane * updated README * Removed finish.adoc include from README.adoc * Updating dependency order * JDK 11 pom update * JDK update * Updated README with [hotspot] changes * Updated README with link in new tab changes * Updated README with link in new tab changes * Updated README * Updated README * Updated README * Updated README.adoc * Adding hotspot and fixing copyright being displayed on right pane * Fixing typo in file number * Minor readme updates * Change the guide attribution to point to master * Removing duplicate dependencies, updating copyrights * Updating copyright
1 parent 02dfdc6 commit 2c22345

File tree

6 files changed

+174
-182
lines changed

6 files changed

+174
-182
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ before_script:
33
- unset _JAVA_OPTIONS
44
- cd finish
55
script:
6-
- mvn clean install
6+
- mvn -q clean install
77
- serverName=$(target/liberty/wlp/bin/server list | cut -d '.' -f2| tr -d '\n');
88
- build=$(grep "Open Liberty" target/liberty/wlp/usr/servers/"$serverName"/logs/console.log
99
| cut -d' ' -f5 | cut -d')' -f1 ); release=$( echo "$build" | cut -d'/' -f1); number=$(

README.adoc

+103-63
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
// Copyright (c) 2017, 2018 IBM Corporation and others.
1+
// Copyright (c) 2017, 2018, 2019 IBM Corporation and others.
22
// Licensed under Creative Commons Attribution-NoDerivatives
33
// 4.0 International (CC BY-ND 4.0)
44
// https://creativecommons.org/licenses/by-nd/4.0/
55
//
66
// Contributors:
77
// IBM Corporation
88
:projectid: cdi-intro
9-
:page-layout: guide
9+
:page-layout: guide-multipane
1010
:page-duration: 15 minutes
1111
:page-releasedate: 2018-03-09
1212
:page-guide-category: microprofile
1313
:page-essential: true
1414
:page-essential-order: 2
1515
:page-description: Learn how to use Contexts and Dependency Injection to manage and inject dependencies into microservices.
16-
:page-tags: ['CDI', 'REST', '@ApplicationScoped', '@RequestScoped', '@Inject', 'MicroProfile', 'Dependency Injection', 'microservices', 'Java EE']
16+
:guide-author: Open Liberty
17+
:page-tags: ['MicroProfile', 'Java EE']
1718
:page-permalink: /guides/{projectid}
1819
:page-related-guides: ['rest-intro']
1920
:common-includes: https://raw.githubusercontent.com/OpenLiberty/guides-common/master
@@ -24,12 +25,11 @@
2425
[.hidden]
2526
NOTE: This repository contains the guide documentation source. To view the guide in published form, view it on the https://openliberty.io/guides/{projectid}.html[Open Liberty website].
2627

27-
Learn how to use Contexts and Dependency Injection to manage and inject dependencies into microservices.
28+
Learn how to use Contexts and Dependency Injection to manage scopes and inject dependencies into microservices.
2829

2930
== What you'll learn
3031

31-
You will learn how to use Contexts and Dependency Injection (CDI) in a simple inventory management application
32-
to manage scopes and inject dependencies.
32+
You will learn how to use Contexts and Dependency Injection (CDI) to manage scopes and inject dependencies in a simple inventory management application.
3333

3434
The application that you will be working with is an `inventory` service,
3535
which stores the information about various JVMs that run on different systems.
@@ -44,9 +44,10 @@ You will also use dependency injection to inject one bean into another to make u
4444
This enables you to inject the bean in its specified context without having to instantiate it yourself.
4545

4646
The implementation of the application and its services are provided for you in the `start/src` directory.
47-
The `system` service can be found in `start/src/main/java/io/openliberty/guides/system`, and the `inventory` service can be found
48-
in `start/src/main/java/io/openliberty/guides/inventory`. If you want to learn more about RESTful web services and how to build them, see
49-
https://openliberty.io/guides/rest-intro.html[Creating a RESTful web service] for details about how to build the `system` service.
47+
The `system` service can be found in the `start/src/main/java/io/openliberty/guides/system` directory,
48+
and the `inventory` service can be found in the `start/src/main/java/io/openliberty/guides/inventory` directory.
49+
If you want to learn more about RESTful web services and how to build them, see
50+
https://openliberty.io/guides/rest-intro.html[Creating a RESTful web service^] for details about how to build the `system` service.
5051
The `inventory` service is built in a similar way.
5152

5253
=== What is CDI?
@@ -62,6 +63,7 @@ controlling exactly when and how these components are instantiated and destroyed
6263
// Getting started
6364
// =================================================================================================
6465

66+
[role='command']
6567
include::{common-includes}/gitclone.adoc[]
6668

6769
=== Try what you'll build
@@ -71,28 +73,28 @@ The `finish` directory in the root of this guide contains the finished inventory
7173
To try out the application, first navigate to the `finish` directory and then run the following
7274
Maven goals to build the application and run it inside Open Liberty:
7375

76+
[role='command']
7477
```
7578
cd finish
7679
mvn install liberty:start-server
7780
```
7881

79-
Point your browser to the http://localhost:9080/inventory/systems URL. This is the starting point of the `inventory`
82+
Point your browser to the http://localhost:9080/inventory/systems[http://localhost:9080/inventory/systems^] URL. This is the starting point of the `inventory`
8083
service and it displays the current contents of the inventory. As you might expect, these are empty since
81-
nothing is stored in the inventory yet. Next, point your browser to the http://localhost:9080/inventory/systems/localhost URL.
84+
nothing is stored in the inventory yet. Next, point your browser to the http://localhost:9080/inventory/systems/localhost[http://localhost:9080/inventory/systems/localhost^] URL.
8285
You see a result in JSON format with the system properties of your local JVM. When you visit this URL, these system
83-
properties are automatically stored in the inventory. Go back to http://localhost:9080/inventory/systems and
86+
properties are automatically stored in the inventory. Go back to http://localhost:9080/inventory/systems[http://localhost:9080/inventory/systems^] and
8487
you see a new entry for `localhost`. For simplicity, only the OS name and username are shown here for
8588
each host. You can repeat this process for your own hostname or any other machine that is running
8689
the `system` service.
8790

8891
After you are done checking out the application, stop the Open Liberty server:
8992

93+
[role='command']
9094
```
9195
mvn liberty:stop-server
9296
```
9397

94-
95-
9698
== Handling dependencies in the application
9799

98100
You will use CDI to inject dependencies into the inventory manager application and learn how to manage the life cycles of your objects.
@@ -101,69 +103,90 @@ You will use CDI to inject dependencies into the inventory manager application a
101103

102104
Navigate to the `start` directory to begin.
103105

104-
Create the inventory manager class in the `src/main/java/io/openliberty/guides/inventory/InventoryManager.java` file:
105-
106-
[source, Java]
106+
[role="code_command hotspot", subs="quotes"]
107+
----
108+
#Create the `InventoryManager` class.#
109+
`src/main/java/io/openliberty/guides/inventory/InventoryManager.java`
107110
----
108-
include::finish/src/main/java/io/openliberty/guides/inventory/InventoryManager.java[tags=**;!copyright;]
111+
InventoryManager.java
112+
[source, Java, linenums, role='code_column']
109113
----
114+
include::finish/src/main/java/io/openliberty/guides/inventory/InventoryManager.java[tags=**;!copyright]
115+
----
116+
117+
This bean contains three simple functions. The [hotspot=21-23 file=0]`get()` function is for retrieving entries from calling the `system` service.
118+
The [hotspot=25-34 file=0]`add()` function is for adding entries to the inventory.
119+
The [hotspot=36-38 file=0]`list()` function is for listing all the entries currently stored in the inventory.
110120

111-
This bean contains three simple functions. The `get()` function is for retrieving entries from calling the `system` service.
112-
The `add()` function is for adding entries to the inventory.
113-
The `list()` function is for listing all the entries currently stored in the inventory.
121+
This bean must be persistent between all of the clients, which means multiple clients need to share the same instance.
122+
To achieve this by using CDI, you can simply add the [hotspot=13 file=0]`@ApplicationScoped` annotation onto the class.
114123

115-
This bean must be persistent between all the clients, which means multiple clients need to share the same instance.
116-
To achieve this by using CDI, you can simply add the `@ApplicationScoped` annotation on the class.
117-
This annotation indicates that this particular
118-
bean is to be initialized once per application.
124+
This annotation indicates that this particular bean is to be initialized once per application.
119125
By making it application-scoped, the container ensures that the same instance of the bean is used whenever
120126
it is injected into the application.
121127

122-
Next, create the `inventory` resource class in the `src/main/java/io/openliberty/guides/inventory/InventoryResource.java` file:
123-
124-
[source, Java]
128+
[role="code_command hotspot", subs="quotes"]
129+
----
130+
#Create the `InventoryResource` class.#
131+
`src/main/java/io/openliberty/guides/inventory/InventoryResource.java`
132+
----
133+
InventoryResource.java
134+
[source, Java, linenums, role='code_column']
125135
----
126136
include::finish/src/main/java/io/openliberty/guides/inventory/InventoryResource.java[tags=**;!copyright]
127137
----
128138

129139
The inventory resource is a RESTful service that is served at the `inventory/systems` endpoint. It enables the client to
130140
communicate with the inventory manager through HTTP methods.
131141

132-
Add the `@RequestScoped` annotation on the class to indicate that this bean is
142+
Add the [hotspot=14 file=1]`@RequestScoped` annotation on the class to indicate that this bean is
133143
to be initialized once for every request. In other words, the bean is instantiated when the request
134144
is received and destroyed when a response is sent back to the client. While this bean can
135145
also be application-scoped, request scope is short-lived and is therefore ideal for HTTP requests.
136146

137147
=== Injecting a dependency
138148

139-
Refer to the `inventory` resource class you created above:
140-
`src/main/java/io/openliberty/guides/inventory/InventoryResource.java`.
149+
Refer to the [hotspot=16-44 file=0]`InventoryResource` class you created above.
141150

142-
The `@Inject` annotation indicates a dependency injection.
151+
The [hotspot=18 file=0]`@Inject` annotation indicates a dependency injection.
143152
You are injecting your `InventoryManager` bean into the `InventoryResource` class.
144153
This injects the bean in its specified context (application-scoped) and makes all of its functionalities
145154
available without the need of instantiating it yourself.
146-
The injected bean can then be invoked directly through the `manager.get(hostname)`
147-
and `manager.list()` function calls.
155+
The injected bean can then be invoked directly through the [hotspot=26 file=0]`manager.get(hostname)`
156+
and [hotspot=42 file=0]`manager.list()` function calls.
148157

149-
Finally, you have a client component `SystemClient` that can be found in `src/main/java/io/openliberty/guides/inventory/client`.
150-
This class communicates with the `system` service to
151-
retrieve the JVM system properties for a particular host that exposes them.
152-
This class also contains detailed Javadocs that you can read for reference.
158+
Finally, you have a client component [hotspot file=1]`SystemClient` that can be found in the
159+
`src/main/java/io/openliberty/guides/inventory/client` directory. This class communicates
160+
with the `system` service to retrieve the JVM system properties for a particular host
161+
that exposes them. This class also contains detailed Javadocs that you can read for reference.
153162

154163
Your inventory application is now completed.
155164

165+
InventoryResource.java
166+
[source, Java, linenums, role='code_column']
167+
----
168+
include::finish/src/main/java/io/openliberty/guides/inventory/InventoryResource.java[tags=**;!copyright]
169+
----
170+
171+
SystemClient.java
172+
[source, Java, linenums, role='code_column']
173+
----
174+
include::finish/src/main/java/io/openliberty/guides/inventory/client/SystemClient.java[tags=**;!copyright]
175+
----
176+
156177
// =================================================================================================
157178
// Building the application
158179
// =================================================================================================
159180

181+
[role='command']
160182
include::{common-includes}/mvnbuild.adoc[]
161183

162184
You can find the `inventory` and `system` services at the following URLs:
163185

164-
- http://localhost:9080/inventory/systems
165-
- http://localhost:9080/system/properties
186+
- http://localhost:9080/inventory/systems[http://localhost:9080/inventory/systems^]
187+
- http://localhost:9080/system/properties[http://localhost:9080/system/properties^]
166188

189+
[role='command']
167190
include::{common-includes}/mvncompile.adoc[]
168191

169192

@@ -179,46 +202,61 @@ Since the application is a RESTful web service application, you can use
179202
JUnit and the RESTful web service Client API to write tests.
180203
In testing the functionality of the application, the scopes and dependencies are being tested.
181204

182-
Create the test class in the `src/test/java/it/io/openliberty/guides/inventory/InventoryEndpointTest.java` file:
183-
184-
[source, Java]
205+
[role="code_command hotspot", subs="quotes"]
185206
----
186-
include::finish/src/test/java/it/io/openliberty/guides/inventory/InventoryEndpointTest.java[tags=**;!copyright;!javadoc]
207+
#Create the `InventoryEndpointTest` class.#
208+
`src/test/java/it/io/openliberty/guides/inventory/InventoryEndpointTest.java`
187209
----
188210

189-
The `@BeforeClass` annotation is placed on a method that runs before any of the test cases.
190-
In this case, the `oneTimeSetup` method retrieves the port number for the Open Liberty server and builds
211+
The [hotspot=28 file=0]`@BeforeClass` annotation is placed on a method that runs before any of the test cases.
212+
In this case, the [hotspot=29-32 file=0]`oneTimeSetup` method retrieves the port number for the Open Liberty server and builds
191213
a base URL string that is used throughout the tests.
192214

193-
The `@Before` and `@After` annotations are placed on methods that run before and after every test case.
194-
These methods are generally used to perform any setup and teardown tasks. In this case, the `setup` method
215+
The [hotspot=34 file=0]`@Before` and [hotspot=40 file=0]`@After` annotations are placed on methods that run before and after every test case.
216+
These methods are generally used to perform any setup and teardown tasks. In this case, the [hotspot=35-38 file=0]`setup` method
195217
creates a JAX-RS client, which makes HTTP requests to the `inventory` service. This client must
196-
also be registered with a JSON-P provider (`JsrJsonpProvider`) to process JSON resources. The `teardown`
218+
also be registered with a JSON-P provider ([hotspot=37 file=0]`JsrJsonpProvider`) to process JSON resources. The [hotspot=41-43 file=0]`teardown`
197219
method simply destroys this client instance.
198220

199221
See the following descriptions of the test cases:
200222

201-
- `testEmptyInventory()` verifies that the inventory is initially empty when the server first starts up.
202-
- `testHostRegistration()` verifies that a host is correctly added to the inventory.
203-
- `testSystemPropertiesMatch()` verifies that the JVM system properties returned by the `system` service match
223+
* [hotspot=53-65 file=0]`testEmptyInventory()` verifies that the inventory is initially empty when the server first starts up.
224+
225+
* [hotspot=67-87 file=0]`testHostRegistration()` verifies that a host is correctly added to the inventory.
226+
227+
* [hotspot=89-115 file=0]`testSystemPropertiesMatch()` verifies that the JVM system properties returned by the `system` service match
204228
the ones stored in the `inventory` service.
205-
- `testUnknownHost()` verifies that an unknown host or a host that does not expose their JVM system
229+
230+
* [hotspot=117-132 file=0]`testUnknownHost()` verifies that an unknown host or a host that does not expose their JVM system
206231
properties is correctly handled as an error.
207232

208-
To force these test cases to run in a particular order, put them in a `testSuite()` method and label
209-
it with a `@Test` annotation so that it automatically runs when your test class run.
233+
To force these test cases to run in a particular order, put them in a [hotspot=46-51 file=0]`testSuite()` method and label
234+
it with a [hotspot=45 file=0]`@Test` annotation so that it automatically runs when your test class run.
210235

211-
Finally, a `system` test class in the `src/test/java/it/io/openliberty/guides/system/SystemEndpointTest.java` file
236+
Finally, the [hotspot file=1]`src/test/java/it/io/openliberty/guides/system/SystemEndpointTest.java` file
212237
is included for you to test the basic functionality of the `system` service.
213238
If a test failure occurs, then you might have introduced a bug into the code.
214239

240+
InventoryEndpointTest.java
241+
[source, Java, linenums, role='code_column']
242+
----
243+
include::finish/src/test/java/it/io/openliberty/guides/inventory/InventoryEndpointTest.java[tags=**;!copyright;!javadoc]
244+
----
245+
246+
SystemEndpointTest.java
247+
[source, Java, linenums, role='code_column']
248+
----
249+
include::finish/src/test/java/it/io/openliberty/guides/system/SystemEndpointTest.java[tags=**;!copyright;!javadoc]
250+
----
251+
215252
// =================================================================================================
216253
// Running the tests
217254
// =================================================================================================
218255

256+
[role='command']
219257
include::{common-includes}/mvnverify.adoc[]
220258

221-
[source, role="no_copy"]
259+
[source, role='no_copy']
222260
----
223261
-------------------------------------------------------
224262
T E S T S
@@ -234,15 +272,17 @@ Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
234272
----
235273

236274
To see whether the tests detect a failure, change the endpoint for the `inventory` service in
237-
the `src/main/java/io/openliberty/guides/inventory/InventoryResource.java` file to something else, then
275+
the [hotspot file=0]`src/main/java/io/openliberty/guides/inventory/InventoryResource.java` file to something else, then
238276
run the Maven build again. You see a test failure occur.
239277

278+
InventoryResource.java
279+
[source, Java, linenums, role='code_column']
280+
----
281+
include::finish/src/main/java/io/openliberty/guides/inventory/InventoryResource.java[tags=**;!copyright]
282+
----
240283

241284
== Great work! You're done!
242285

243-
You have just completed building a simple inventory application on top of Open Liberty using CDI services.
244-
245-
You can continue to try one of the related guides, which demonstrate new technologies that you can learn and
246-
expand on top what you built in this guide.
286+
You have just completed building a simple inventory application using CDI services in Open Liberty.
247287

248-
include::{common-includes}/finish.adoc[]
288+
include::{common-includes}/attribution.adoc[subs="attributes"]

0 commit comments

Comments
 (0)