Skip to content

Commit

Permalink
issue #260 embedded tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
oraNod authored and tristantarrant committed Jul 5, 2022
1 parent 12b06c9 commit 1e0e609
Show file tree
Hide file tree
Showing 13 changed files with 346 additions and 401 deletions.
6 changes: 3 additions & 3 deletions _includes/embedded-tutorial-menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ <h4>Tutorial Steps</h4>
<li> <a href="step-7">Listen to changes in the cache</a> </li>
<li> <a href="step-8">Grouping entries together</a> </li>
<li> <a href="step-9">Temperature averages with streams</a> </li>
<li> <a href="step-10">Declarative configuration</a> </li> </ol>
<li> <a href="step-10">Declarative configuration</a> </li>
</ol>

<!--
<!--
%ol{:start => "0" }
- @tutorial_steps.each_with_index do |step, index|
%li
%a{:href => step_prefix + "step-" + (index).to_s } #{step} -->
%a{:href => step_prefix + "step-" + (index).to_s } #{step} -->
9 changes: 8 additions & 1 deletion _sass/layouts/tutorial.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
.tutorial > .content,
.tutorial-extended > .content {
padding-top: 0;
}
}

pre code {
display: inline;
background: none;
white-space: pre-line;
padding: 0;
}
97 changes: 41 additions & 56 deletions tutorials/embedded/step-0.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,68 +15,55 @@
<div class="width-9-12 width-12-12-m">
<h4>{{page.title}}</h4>
<p>
Let's begin by checking out the project. Fire up your terminal and type:
Let's begin by checking out the project. Fire up your terminal and enter:
</p>
<pre>
<code class="nohighlight">
= preserve do
:escaped
$ git clone https://github.com/infinispan/infinispan-embedded-tutorial.git
$ cd infinispan-embedded-tutorial
git clone https://github.com/infinispan/infinispan-embedded-tutorial.git && cd infinispan-embedded-tutorial
</code>
</pre>
<p>
The above commands will create a local copy (clone) of the git repository and change the current directory so that all subsequent commands will run from the project root directory. We now want to move to "Step 0" in the project:
</p>
<pre>
<code class="nohighlight">
= preserve do
:escaped
$ git checkout -f step-0
git checkout -f step-0
</code>
</pre>
<p>
The initial step is just a plain Maven Java project with an implementation of our Weather Application. The application connects to the <a href="http://openweathermap.org">OpenWeatherMap</a> API to retrieve current weather data for a variety of cities. Usage of the OpenWeatherMap API requires registering for a free API key. Before launching the application, ensure you've set the OWMAPIKEY environment variable to your API key:
</p>
<pre>
<code class="nohighlight">
= preserve do
:escaped
$ export OWMAPIKEY=111111
export OWMAPIKEY=111111
</code>
</pre>
<p>
In Windows you'd use:
</p>
<pre>
<code class="nohighlight">
= preserve do
:escaped
C:\> set OWMAPIKEY=111111
C:\> set OWMAPIKEY=111111
</code>
</pre>
</p>
<p>
If you'd rather not register, if you don't have an Internet connection or you are having trouble connecting to the service, don't worry: the application will use a random weather service. This initial step has no dependencies and uses JDK logging. The following image shows the layout of the project:
</p>
%img{:src => "../infinispan-embedded-tree.png"}
<img src="{{site.baseurl}}/tutorials/embedded/infinispan-embedded-tree.png">
<p>
Let's compile and run it:
</p>
<pre>
<code class="nohighlight">
= preserve do
:escaped
$ mvn clean package
mvn clean package
</code>
</pre>
<p>
Maven will run both goals specified on the command-line in order. The first goal (clean) just cleans any previously generated files and the second one (package) compiles the project, runs any unit tests and packages the resulting code into a Jar file. At this point we can also run the code. The <i>main()</i> method of the <i>WeatherApp</i> class initializes the WeatherService and retrieves the current temperature for the list of cities twice and prints the elapsed time after each retrieval. Since no caching is being done at this stage, the application is quite slow on both runs, since it always has to retrieve the information.
</p>
<pre>
<code class="nohighlight">
= preserve do
:escaped
$ mvn exec:exec
mvn exec:exec
</code>
</pre>

Expand All @@ -85,40 +72,38 @@ <h4>{{page.title}}</h4>
<div>
<pre>
<code class="nohighlight">
= preserve do
:escaped
---- Fetching weather information ----
Rome, Italy - Temperature: 12.9° C, Conditions: light rain
Como, Italy - Temperature: 6.3° C, Conditions: Sky is Clear
Basel, Switzerland - Temperature: 0.8° C, Conditions: overcast clouds
Bern, Switzerland - Temperature: -1.6° C, Conditions: broken clouds
London, UK - Temperature: 1.8° C, Conditions: light rain
Newcastle, UK - Temperature: 2.6° C, Conditions: scattered clouds
Bucharest, Romania - Temperature: 9.3° C, Conditions: scattered clouds
Cluj-Napoca, Romania - Temperature: 6.4° C, Conditions: scattered clouds
Ottawa, Canada - Temperature: -7.0° C, Conditions: overcast clouds
Toronto, Canada - Temperature: -7.0° C, Conditions: broken clouds
Lisbon, Portugal - Temperature: 14.6° C, Conditions: overcast clouds
Porto, Portugal - Temperature: 12.2° C, Conditions: moderate rain
Raleigh, USA - Temperature: 3.9° C, Conditions: Sky is Clear
Washington, USA - Temperature: 3.4° C, Conditions: light rain
---- Fetched in 2210ms ----
---- Fetching weather information ----
Rome, Italy - Temperature: 12.9° C, Conditions: light rain
Como, Italy - Temperature: 6.3° C, Conditions: Sky is Clear
Basel, Switzerland - Temperature: 0.8° C, Conditions: overcast clouds
Bern, Switzerland - Temperature: -1.6° C, Conditions: broken clouds
London, UK - Temperature: 1.8° C, Conditions: light rain
Newcastle, UK - Temperature: 2.6° C, Conditions: scattered clouds
Bucharest, Romania - Temperature: 9.3° C, Conditions: scattered clouds
Cluj-Napoca, Romania - Temperature: 6.4° C, Conditions: scattered clouds
Ottawa, Canada - Temperature: -7.0° C, Conditions: overcast clouds
Toronto, Canada - Temperature: -7.0° C, Conditions: broken clouds
Lisbon, Portugal - Temperature: 14.6° C, Conditions: overcast clouds
Porto, Portugal - Temperature: 12.2° C, Conditions: moderate rain
Raleigh, USA - Temperature: 3.9° C, Conditions: Sky is Clear
Washington, USA - Temperature: 3.4° C, Conditions: light rain
---- Fetched in 1820ms ----
---- Fetching weather information ----
Rome, Italy - Temperature: 12.9° C, Conditions: light rain
Como, Italy - Temperature: 6.3° C, Conditions: Sky is Clear
Basel, Switzerland - Temperature: 0.8° C, Conditions: overcast clouds
Bern, Switzerland - Temperature: -1.6° C, Conditions: broken clouds
London, UK - Temperature: 1.8° C, Conditions: light rain
Newcastle, UK - Temperature: 2.6° C, Conditions: scattered clouds
Bucharest, Romania - Temperature: 9.3° C, Conditions: scattered clouds
Cluj-Napoca, Romania - Temperature: 6.4° C, Conditions: scattered clouds
Ottawa, Canada - Temperature: -7.0° C, Conditions: overcast clouds
Toronto, Canada - Temperature: -7.0° C, Conditions: broken clouds
Lisbon, Portugal - Temperature: 14.6° C, Conditions: overcast clouds
Porto, Portugal - Temperature: 12.2° C, Conditions: moderate rain
Raleigh, USA - Temperature: 3.9° C, Conditions: Sky is Clear
Washington, USA - Temperature: 3.4° C, Conditions: light rain
---- Fetched in 2210ms ----
---- Fetching weather information ----
Rome, Italy - Temperature: 12.9° C, Conditions: light rain
Como, Italy - Temperature: 6.3° C, Conditions: Sky is Clear
Basel, Switzerland - Temperature: 0.8° C, Conditions: overcast clouds
Bern, Switzerland - Temperature: -1.6° C, Conditions: broken clouds
London, UK - Temperature: 1.8° C, Conditions: light rain
Newcastle, UK - Temperature: 2.6° C, Conditions: scattered clouds
Bucharest, Romania - Temperature: 9.3° C, Conditions: scattered clouds
Cluj-Napoca, Romania - Temperature: 6.4° C, Conditions: scattered clouds
Ottawa, Canada - Temperature: -7.0° C, Conditions: overcast clouds
Toronto, Canada - Temperature: -7.0° C, Conditions: broken clouds
Lisbon, Portugal - Temperature: 14.6° C, Conditions: overcast clouds
Porto, Portugal - Temperature: 12.2° C, Conditions: moderate rain
Raleigh, USA - Temperature: 3.9° C, Conditions: Sky is Clear
Washington, USA - Temperature: 3.4° C, Conditions: light rain
---- Fetched in 1820ms ----
</code>
</pre>
</div>
Expand Down
39 changes: 16 additions & 23 deletions tutorials/embedded/step-1.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@ <h4>{{page.title}}</h4>
Now that we have our simple application, let's add Infinispan to it and initialize a CacheManager. Adding Infinispan is achieved by declaring the following dependency in the pom.xml file:
</p>
<pre>
<code class="xml">
= preserve do
:escaped
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
<version>9.4.0.Final</version>
</dependency>
</code>
&lt;dependency&gt;
&lt;groupId&gt;org.infinispan&lt;/groupId&gt;
&lt;artifactId&gt;infinispan-core&lt;/artifactId&gt;
&lt;version&gt;{{ site.data.ispn.stable.maven_latest}}&lt;/version&gt;
&lt;/dependency&gt;
</pre>
<p>
Now we can add a CacheManager. The CacheManager has many purposes:
Expand All @@ -34,41 +30,38 @@ <h4>{{page.title}}</h4>
<li>it manages global configuration and common data structures and resources (e.g. thread pools)</li>
<li>it manages clustering</li>
</ul>
<br/>
<br/>
A CacheManager is a fairly heavy-weight component, and you will probably want to initialize it early on in your application lifecycle.
</p>
<p>
A CacheManager is a fairly heavy-weight component, and you will probably want to initialize it early on in your application lifecycle.
<br/>
A CacheManager is simply initialized by constructing it as follows:
</p>
<pre>
<code class="java">
= preserve do
:escaped
EmbeddedCacheManager cacheManager = new DefaultCacheManager();
</code>
</pre>
<p>
This will give us a default local (i.e. non-clustered) CacheManager. Because a CacheManager holds on to some resources which require proper disposal, you also need to make sure you stop it when you don't need it anymore. This is done by invoking the <i>stop()</i> method, as follows:
</p>
<pre>
<code class="java">
= preserve do
:escaped
cacheManager.stop();
</code>
</pre>
Once a CacheManager has been stopped it, and all resources obtained from it, cannot be used anymore.
</p>
<p>
Once a CacheManager has been stopped it, and all resources obtained from it, cannot be used anymore.
<br />
If you attempt to compile and run the application at this step
</p>
<pre>
<code class="nohighlight">
= preserve do
:escaped
$ git checkout step-1
$ mvn clean package exec:exec
git checkout step-1
mvn clean package exec:exec
</code>
</pre>
<p>
you will see that nothing has changed. This is expected: we are not doing anything with the CacheManager yet. We will put it to good use in the next step.
You will see that nothing has changed. This is expected: we are not doing anything with the CacheManager yet. We will put it to good use in the next step.
</p>
</div>
</div>
54 changes: 24 additions & 30 deletions tutorials/embedded/step-10.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,42 @@
<h4>{{page.title}}</h4>
<p>
Up to now we have been configuring Infinispan through it's programmatic API, but it is also typical for applications to separate code from configuration. For this purpose we can use the declarative configuration, whereby all aspects of Infinispan can be configured using an external XML file. First of all let's convert the configuration into XML format:
</p>
<pre>
<code class="xml">
= preserve do
:escaped
<?xml version="1.0" encoding="UTF-8"?>
<infinispan
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;infinispan
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:7.1 http://www.infinispan.org/schemas/infinispan-config-7.1.xsd"
xmlns="urn:infinispan:config:7.1">

<cache-container default-cache="default">
<transport cluster="WeatherApp"/>

<distributed-cache name="default" mode="SYNC">
<expiration lifespan="5000"/>
<groups enabled="true">
<grouper class="org.infinispan.tutorial.embedded.LocationWeather$LocationGrouper"/>
</groups>
</distributed-cache>
</cache-container>
</infinispan>
</code>
xsi:schemaLocation="urn:infinispan:config:{{site.data.ispn.stable.minor_version}} http://www.infinispan.org/schemas/infinispan-config-{{site.data.ispn.stable.minor_version}}.xsd"
xmlns="urn:infinispan:config:{{site.data.ispn.stable.minor_version}}"&gt;

&lt;cache-container default-cache="default"&gt;
&lt;transport cluster="WeatherApp"/&gt;

&lt;distributed-cache name="default" mode="SYNC"&gt;
&lt;expiration lifespan="5000"/&gt;
&lt;groups enabled="true"&gt;
&lt;grouper class="org.infinispan.tutorial.embedded.LocationWeather$LocationGrouper"/&gt;
&lt;/groups&gt;
&lt;/distributed-cache&gt;
&lt;/cache-container&gt;
&lt;/infinispan&gt;
</pre>
<p>
By placing this file in Maven's <i>src/main/resources</i> folder, it will be available in the classpath, so that we can use it to configure the <i>DefaultCacheManager</i> as follows:
</p>
<pre>
<code class="java">
= preserve do
:escaped
cacheManager = new DefaultCacheManager(WeatherApp.class.getResourceAsStream("/weatherapp-infinispan.xml"));
cacheManager = new DefaultCacheManager(WeatherApp.class.getResourceAsStream("/weatherapp-infinispan.xml"));
</code>
</pre>
</pre>
You are now ready to run the code:
<pre>
<code class="nohighlight">
= preserve do
:escaped
$ git checkout -f step-11
$ mvn clean package exec:exec # from terminal 1
$ mvn exec:exec # from terminal 2
git checkout -f step-11
mvn clean package exec:exec # from terminal 1
mvn exec:exec # from terminal 2
</code>
</pre>
</p>
<p>
Obviously, the output will be identical to the previous run, which is what we were expecting !
</p>
Expand Down
Loading

0 comments on commit 1e0e609

Please sign in to comment.