@@ -19,8 +19,8 @@ a number of Java EE (and related) technologies.
19
19
== Remoting and Web Services with Spring
20
20
21
21
Spring features integration classes for remoting support with various technologies.
22
- The remoting support eases the development of remote-enabled services, implemented by
23
- your usual POJOs. Currently, Spring supports the following remoting technologies:
22
+ The remoting support eases the development of remote-enabled services, implemented
23
+ by your usual POJOs. Currently, Spring supports the following remoting technologies:
24
24
25
25
* *Remote Method Invocation (RMI)*: Through the use of `RmiProxyFactoryBean` and
26
26
`RmiServiceExporter`, Spring supports both traditional RMI (with `java.rmi.Remote`
@@ -34,10 +34,11 @@ your usual POJOs. Currently, Spring supports the following remoting technologies
34
34
`HessianServiceExporter`, you can transparently expose your services through the
35
35
lightweight binary HTTP-based protocol provided by Caucho.
36
36
* *JAX-WS*: Spring provides remoting support for web services through JAX-WS.
37
- * *JMS*: Remoting by using JMS as the underlying protocol is supported through the
38
- `JmsInvokerServiceExporter` and `JmsInvokerProxyFactoryBean` classes.
39
- * *AMQP*: Remoting by using AMQP as the underlying protocol is supported by the Spring
40
- AMQP project.
37
+ * *JMS*: Remoting via JMS as the underlying protocol is supported through the
38
+ `JmsInvokerServiceExporter` and `JmsInvokerProxyFactoryBean` classes in the
39
+ `spring-jms` module.
40
+ * *AMQP*: Remoting via AMQP as the underlying protocol is supported by the
41
+ separate Spring AMQP project.
41
42
42
43
While discussing the remoting capabilities of Spring, we use the following domain
43
44
model and corresponding services:
@@ -86,9 +87,9 @@ model and corresponding services:
86
87
}
87
88
----
88
89
89
- This section starts by exposing the service to a remote client by using RMI and talk a bit about
90
- the drawbacks of using RMI. It then continues with an example that uses Hessian as the
91
- protocol.
90
+ This section starts by exposing the service to a remote client by using RMI and talk a bit
91
+ about the drawbacks of using RMI. It then continues with an example that uses Hessian as
92
+ the protocol.
92
93
93
94
94
95
@@ -141,8 +142,8 @@ The following example shows how to do so:
141
142
In the preceding example, we override the port for the RMI registry. Often, your application
142
143
server also maintains an RMI registry, and it is wise to not interfere with that one.
143
144
Furthermore, the service name is used to bind the service. So, in the preceding example, the
144
- service is bound at `'rmi://HOST:1199/AccountService'`. We use this URL later on
145
- to link in the service at the client side.
145
+ service is bound at `'rmi://HOST:1199/AccountService'`. We use this URL later on to link in
146
+ the service at the client side.
146
147
147
148
NOTE: The `servicePort` property has been omitted (it defaults to 0). This means that an
148
149
anonymous port is used to communicate with the service.
@@ -6776,16 +6777,17 @@ You can find a further explanation of the differences between a buffer and a cac
6776
6777
https://en.wikipedia.org/wiki/Cache_(computing)#The_difference_between_buffer_and_cache[here].
6777
6778
****
6778
6779
6779
- At its core, the cache abstraction applies caching to Java methods, thus reducing the number
6780
- of executions based on the information available in the cache. That is, each time a
6781
- targeted method is invoked, the abstraction applies a caching behavior that checks
6782
- whether the method has been already executed for the given arguments. If it has been executed,
6783
- the cached result is returned without having to execute the actual method. If the method has
6784
- not been executed, then it is executed, and the result is cached and returned to the user so that, the
6785
- next time the method is invoked, the cached result is returned. This way, expensive
6786
- methods (whether CPU- or IO-bound) can be executed only once for a given set of
6787
- parameters and the result reused without having to actually execute the method again.
6788
- The caching logic is applied transparently without any interference to the invoker.
6780
+ At its core, the cache abstraction applies caching to Java methods, thus reducing the
6781
+ number of executions based on the information available in the cache. That is, each time
6782
+ a targeted method is invoked, the abstraction applies a caching behavior that checks
6783
+ whether the method has been already executed for the given arguments. If it has been
6784
+ executed, the cached result is returned without having to execute the actual method.
6785
+ If the method has not been executed, then it is executed, and the result is cached and
6786
+ returned to the user so that, the next time the method is invoked, the cached result is
6787
+ returned. This way, expensive methods (whether CPU- or IO-bound) can be executed only
6788
+ once for a given set of parameters and the result reused without having to actually
6789
+ execute the method again. The caching logic is applied transparently without any
6790
+ interference to the invoker.
6789
6791
6790
6792
IMPORTANT: This approach works only for methods that are guaranteed to return the same
6791
6793
output (result) for a given input (or arguments) no matter how many times it is executed.
@@ -6794,12 +6796,11 @@ The caching abstraction provides other cache-related operations, such as the abi
6794
6796
to update the content of the cache or to remove one or all entries. These are useful if
6795
6797
the cache deals with data that can change during the course of the application.
6796
6798
6797
- As with other services in the Spring Framework, the caching service is an
6798
- abstraction (not a cache implementation) and requires the use of actual storage to
6799
- store the cache data -- that is, the abstraction frees you from having to write
6800
- the caching logic but does not provide the actual data store. This abstraction is
6801
- materialized by the `org.springframework.cache.Cache` and
6802
- `org.springframework.cache.CacheManager` interfaces.
6799
+ As with other services in the Spring Framework, the caching service is an abstraction
6800
+ (not a cache implementation) and requires the use of actual storage to store the cache data --
6801
+ that is, the abstraction frees you from having to write the caching logic but does not
6802
+ provide the actual data store. This abstraction is materialized by the
6803
+ `org.springframework.cache.Cache` and `org.springframework.cache.CacheManager` interfaces.
6803
6804
6804
6805
Spring provides <<cache-store-configuration, a few implementations>> of that abstraction:
6805
6806
JDK `java.util.concurrent.ConcurrentMap` based caches, https://www.ehcache.org/[Ehcache 2.x],
@@ -6844,11 +6845,12 @@ For caching declaration, Spring's caching abstraction provides a set of Java ann
6844
6845
[[cache-annotations-cacheable]]
6845
6846
==== The `@Cacheable` Annotation
6846
6847
6847
- As the name implies, you can use `@Cacheable` to demarcate methods that are cacheable -- that
6848
- is, methods for which the result is stored in the cache so that, on subsequent invocations
6849
- (with the same arguments), the value in the cache is returned without having to actually
6850
- execute the method. In its simplest form, the annotation declaration requires the name
6851
- of the cache associated with the annotated method, as the following example shows:
6848
+ As the name implies, you can use `@Cacheable` to demarcate methods that are cacheable --
6849
+ that is, methods for which the result is stored in the cache so that, on subsequent
6850
+ invocations (with the same arguments), the value in the cache is returned without
6851
+ having to actually execute the method. In its simplest form, the annotation declaration
6852
+ requires the name of the cache associated with the annotated method, as the following
6853
+ example shows:
6852
6854
6853
6855
[source,java,indent=0]
6854
6856
[subs="verbatim,quotes"]
@@ -6860,10 +6862,9 @@ of the cache associated with the annotated method, as the following example show
6860
6862
In the preceding snippet, the `findBook` method is associated with the cache named `books`.
6861
6863
Each time the method is called, the cache is checked to see whether the invocation has
6862
6864
already been executed and does not have to be repeated. While in most cases, only one
6863
- cache is declared, the annotation lets multiple names be specified so that more
6864
- than one cache is being used. In this case, each of the caches is checked before
6865
- executing the method -- if at least one cache is hit, the associated value is
6866
- returned.
6865
+ cache is declared, the annotation lets multiple names be specified so that more than one
6866
+ cache is being used. In this case, each of the caches is checked before executing the
6867
+ method -- if at least one cache is hit, the associated value is returned.
6867
6868
6868
6869
NOTE: All the other caches that do not contain the value are also updated, even though
6869
6870
the cached method was not actually executed.
@@ -6880,17 +6881,17 @@ The following example uses `@Cacheable` on the `findBook` method:
6880
6881
[[cache-annotations-cacheable-default-key]]
6881
6882
===== Default Key Generation
6882
6883
6883
- Since caches are essentially key-value stores, each invocation of a cached method needs
6884
- to be translated into a suitable key for cache access. The caching
6885
- abstraction uses a simple `KeyGenerator` based on the following algorithm:
6884
+ Since caches are essentially key-value stores, each invocation of a cached method
6885
+ needs to be translated into a suitable key for cache access. The caching abstraction
6886
+ uses a simple `KeyGenerator` based on the following algorithm:
6886
6887
6887
6888
* If no params are given, return `SimpleKey.EMPTY`.
6888
6889
* If only one param is given, return that instance.
6889
6890
* If more than one param is given, return a `SimpleKey` that contains all parameters.
6890
6891
6891
6892
This approach works well for most use-cases, as long as parameters have natural keys
6892
- and implement valid `hashCode()` and `equals()` methods. If that is not the case, you need to change the
6893
- strategy.
6893
+ and implement valid `hashCode()` and `equals()` methods. If that is not the case,
6894
+ you need to change the strategy.
6894
6895
6895
6896
To provide a different default key generator, you need to implement the
6896
6897
`org.springframework.cache.interceptor.KeyGenerator` interface.
@@ -6956,8 +6957,9 @@ The preceding snippets show how easy it is to select a certain argument, one of
6956
6957
properties, or even an arbitrary (static) method.
6957
6958
6958
6959
If the algorithm responsible for generating the key is too specific or if it needs
6959
- to be shared, you can define a custom `keyGenerator` on the operation. To do
6960
- so, specify the name of the `KeyGenerator` bean implementation to use, as the following example shows:
6960
+ to be shared, you can define a custom `keyGenerator` on the operation. To do so,
6961
+ specify the name of the `KeyGenerator` bean implementation to use, as the following
6962
+ example shows:
6961
6963
6962
6964
[source,java,indent=0]
6963
6965
[subs="verbatim,quotes"]
@@ -6985,8 +6987,8 @@ To provide a different default cache resolver, you need to implement the
6985
6987
The default cache resolution fits well for applications that work with a
6986
6988
single `CacheManager` and have no complex cache resolution requirements.
6987
6989
6988
- For applications that work with several cache managers, you can
6989
- set the `cacheManager` to use for each operation, as the following example shows:
6990
+ For applications that work with several cache managers, you can set the
6991
+ `cacheManager` to use for each operation, as the following example shows:
6990
6992
6991
6993
[source,java,indent=0]
6992
6994
[subs="verbatim,quotes"]
@@ -7034,8 +7036,8 @@ of caching.
7034
7036
7035
7037
For those particular cases, you can use the `sync` attribute to instruct the underlying
7036
7038
cache provider to lock the cache entry while the value is being computed. As a result,
7037
- only one thread is busy computing the value, while the others are blocked until the
7038
- entry is updated in the cache. The following example shows how to use the `sync` attribute:
7039
+ only one thread is busy computing the value, while the others are blocked until the entry
7040
+ is updated in the cache. The following example shows how to use the `sync` attribute:
7039
7041
7040
7042
[source,java,indent=0]
7041
7043
[subs="verbatim,quotes"]
@@ -7046,8 +7048,8 @@ entry is updated in the cache. The following example shows how to use the `sync`
7046
7048
<1> Using the `sync` attribute.
7047
7049
7048
7050
7049
- NOTE: This is an optional feature, and your favorite cache library may not support it. All
7050
- `CacheManager` implementations provided by the core framework support it. See the
7051
+ NOTE: This is an optional feature, and your favorite cache library may not support it.
7052
+ All `CacheManager` implementations provided by the core framework support it. See the
7051
7053
documentation of your cache provider for more details.
7052
7054
7053
7055
[[cache-annotations-cacheable-condition]]
@@ -7161,11 +7163,11 @@ available to the context so that you can use them for key and conditional comput
7161
7163
[[cache-annotations-put]]
7162
7164
==== The `@CachePut` Annotation
7163
7165
7164
- When the cache needs to be updated without interfering with the method
7165
- execution, you can use the `@CachePut` annotation. That is, the method is always
7166
- executed and its result is placed into the cache (according to the `@CachePut` options). It
7167
- supports the same options as `@Cacheable` and should be used for cache population rather
7168
- than method flow optimization. The following example uses the `@CachePut` annotation:
7166
+ When the cache needs to be updated without interfering with the method execution,
7167
+ you can use the `@CachePut` annotation. That is, the method is always executed and its
7168
+ result is placed into the cache (according to the `@CachePut` options). It supports
7169
+ the same options as `@Cacheable` and should be used for cache population rather than
7170
+ method flow optimization. The following example uses the `@CachePut` annotation:
7169
7171
7170
7172
[source,java,indent=0]
7171
7173
[subs="verbatim,quotes"]
@@ -7177,11 +7179,11 @@ than method flow optimization. The following example uses the `@CachePut` annota
7177
7179
IMPORTANT: Using `@CachePut` and `@Cacheable` annotations on the same method is generally
7178
7180
strongly discouraged because they have different behaviors. While the latter causes the
7179
7181
method execution to be skipped by using the cache, the former forces the execution in
7180
- order to execute a cache update. This leads to unexpected behavior and, with the exception of
7181
- specific corner-cases (such as annotations having conditions that exclude them from each
7182
+ order to execute a cache update. This leads to unexpected behavior and, with the exception
7183
+ of specific corner-cases (such as annotations having conditions that exclude them from each
7182
7184
other), such declarations should be avoided. Note also that such conditions should not rely
7183
- on the result object (that is, the `#result` variable), as these are validated up-front to confirm
7184
- the exclusion.
7185
+ on the result object (that is, the `#result` variable), as these are validated up-front to
7186
+ confirm the exclusion.
7185
7187
7186
7188
7187
7189
[[cache-annotations-evict]]
@@ -7207,11 +7209,11 @@ all entries from the `books` cache:
7207
7209
<1> Using the `allEntries` attribute to evict all entries from the cache.
7208
7210
7209
7211
7210
- This option comes in handy when an entire cache region needs to be cleared out. Rather
7211
- than evicting each entry (which would take a long time, since it is inefficient), all the
7212
- entries are removed in one operation, as the preceding example shows. Note that the framework ignores
7213
- any key specified in this scenario as it does not apply (the entire cache is evicted, not
7214
- only one entry).
7212
+ This option comes in handy when an entire cache region needs to be cleared out.
7213
+ Rather than evicting each entry (which would take a long time, since it is inefficient),
7214
+ all the entries are removed in one operation, as the preceding example shows.
7215
+ Note that the framework ignores any key specified in this scenario as it does not apply
7216
+ (the entire cache is evicted, not only one entry).
7215
7217
7216
7218
You can also indicate whether the eviction should occur after (the default) or before
7217
7219
the method executes by using the `beforeInvocation` attribute. The former provides the
@@ -7222,10 +7224,9 @@ The latter (`beforeInvocation=true`) causes the eviction to always occur before
7222
7224
method is invoked. This is useful in cases where the eviction does not need to be tied
7223
7225
to the method outcome.
7224
7226
7225
- Note that `void` methods can be used with `@CacheEvict` - as the
7226
- methods act as a trigger, the return values are ignored (as they do not interact with the
7227
- cache). This is not the case with `@Cacheable` which adds or updates data into the cache
7228
- and, thus, requires a result.
7227
+ Note that `void` methods can be used with `@CacheEvict` - as the methods act as a trigger,
7228
+ the return values are ignored (as they do not interact with the cache). This is not the case
7229
+ with `@Cacheable` which adds or updates data into the cache and, thus, requires a result.
7229
7230
7230
7231
7231
7232
[[cache-annotations-caching]]
@@ -7714,9 +7715,9 @@ or eviction contracts.
7714
7715
7715
7716
NOTE: Ehcache 3.x is fully JSR-107 compliant and no dedicated support is required for it.
7716
7717
7717
- The Ehcache 2.x implementation is located in the `org.springframework.cache.ehcache` package.
7718
- Again, to use it, you need to declare the appropriate `CacheManager`. The following
7719
- example shows how to do so:
7718
+ The Ehcache 2.x implementation is located in the `org.springframework.cache.ehcache`
7719
+ package. Again, to use it, you need to declare the appropriate `CacheManager`.
7720
+ The following example shows how to do so:
7720
7721
7721
7722
[source,xml,indent=0]
7722
7723
[subs="verbatim,quotes"]
0 commit comments