顾睿

                <h2 pid="2">What is Redisson?</h2>

<p pid="2"><a href="https://github.com/mrniko/redisson" rel="nofollow" target="_blank">Redisson</a> is a Redis Java library that provides distributed Java objects and services including Set, Multimap, SortedSet, Map, List, Queue, BlockingQueue, Deque, BlockingDeque, Semaphore, Lock, AtomicLong, CountDownLatch, Publish / Subscribe, Bloom filter, Remote service, Spring cache, Executor service, Live Object service and most recently added Scheduled executor service on top of the Redis server.</p>
<h2>What is a Live Object?</h2>
<p pid="3">A Live Object can be understood as an enhanced version of standard Java object, of which an instance reference can be shared not only between threads in a single JVM, but can also be shared between different JVMs across different machines. Wikipedia discribes it as:</p>
<blockquote pid="4">
Live distributed object (also abbreviated as live object) refers to a running instance of a distributed multi-party (or peer-to-peer) protocol, viewed from the object-oriented perspective, as an entity that has a distinct identity, may encapsulate internal state and threads of execution, and that exhibits a well-defined externally visible behavior.
</blockquote>
<h2>How Redisson Live Object Works</h2>
<p pid="6">Redisson Live Object (RLO) realized this idea by mapping all the fields inside a Java class to a Redis hash through a runtime-constructed proxy class. All the get/set methods of each field are translated to hget/hset commands operated on the Redis hash, making it accessable to/from any clients connected to the same Redis server. As we all know, the field values of an object represent its state; having them stored in a remote repository, Redis, makes it a distributed object. This object is a Redisson Live Object.</p>
<h2>What Benefits Does it Have Over a Standard Java Object?</h2>
<p pid="7">This process provides a range of benefit over standard Java objects. Traditionally, when multiple threads access a shared object, the reference of the object is then passed to each of them. This model is fine for standalone applications, but it's not possible to share the object between applications and/or servers in a distributed environment. For those types of use cases, we would have to serialize the object, transmit it to the other end and deserialize it back to an object. It is often the case that the entire object has to be serialized and transmitted even just one field is modified. This process not only adds performance overhead to the application, it also complicates the programming model: A serialized object is detached from its original, so changes to the state of the object which happen after serialisation are not visible to the other end. </p>
<p pid="8">By using RLO, sharing an object between applications and/or servers is the same as sharing one in a standalone application. This removes the need for serialization and deserialization, and at the same time reduces the complexity of the programming model: Changes made to one field is (almost^) immediately accessable to other processes, applications and servers. (^Redis' eventual consistant replication rule still applies when connected to slave nodes)</p>
<p pid="9">Since the Redis server is a single-threaded application, all field access to the live object is automatically executed in atomic fashion: a value will not be changed when you are reading it.</p>
<p pid="10">With Redisson Live Object, you can treat the redis server as a shared Heap space for all connected JVMs.</p>
<h2>How is a Live Object Different Than a Standard Java Object?</h2>
<p pid="11">Due to the fact that the state of the object is kept in Redis instead of in the local JVM, there are a few differences compared to using standard Java objects, and they need to be taken into consideration when architecting a system that employs this feature. </p>
<p pid="12">As described above, in a Redisson Live Object, changes to its fields are immediately made available to other processes and JVMs. This in effect makes all fields in the object volatile. </p>
<h2>What Kind of Application Can Benefit From It?</h2>
<p pid="13">Redisson Live Object can prove itself to be useful in many domains and fields. It can assist developers in easily creating team-collabrating systems, such as a whiteboard, mobile game matchmaking, cross device copy-and-paste like you've seen in the upcoming iOS 10, and many others.</p>
<p pid="14">It can be used to improve availability of a system or service while reducing the complexity of its implementation. Having critical system states and/or configurations stored as live objects in a redis cluster, the system can survive from node crashes without complicated logic to achieve background state syncing and config validating upon reviving. This is extremely useful for mission critical systems in the Oil and Gas industry, Energy industry, Banking/Finance industry, and many more. </p>
<p pid="15">It can also be used to manage the state of swarms of connected devices. By connecting multiple devices together to a shared live object, we can easily make dumb switches and devices into a smart connected self-aware network.</p>
<p pid="16">While the Redisson Live Object seems incredibly useful on its own, with careful design and engineering, it can be used to create some features which are even greater. By combining it with Redisson Remote Service, we can create applications that can be paused, fast-forwarded and even rewound and replayed on demand. By constructing all the application states as live objects, you can kill all running process and have the application "Paused"; Or simply run multiple copies of the same application on multiple nodes to reduce the load on each individual node and/or increase the overall performance of the system. This effectively makes the system go "Fast-forward"; Or you can keep snapshots of all the live objects every so often, while keeping all the changes tracked in a log, you can have the application go back in time, relive a moment in the past and replay all the changes as you wish. This is making the system "Rewind and Replay". It's like elastic computing happening at the application level.</p>
<h2>How Does it Differ From Other Live Objects?</h2>
<p pid="17">Redisson Live Object was designed and developed by Rui Gu overseen by Nikita Koksharov. The idea was inspired originally by the Java JPA API since both the RDBMs and redis are centralized services and used as data repositories. It was initially called "Attached Object" since its behavior was somewhat similar to the JPA entity object inside a JPA transaction. </p>
<p pid="18">Nikita Koksharov suggested the name "Live Object" and we both think it is more appropriate for the final shape of the feature than "Attached Object".</p>
<p pid="19">Knowing the evolution of this feature, it is understandable that while sharing the same Live Object design pattern with solutions from other projects, Redisson Live Object works differently internally to those solutions in a few ways. Redisson Live Object uses redis as its data storage, all changes to the object are translated as redis commands and operated on a given redis hash. The local JVM does not hold any value in the fields of the object except for the field that represents the key name of the hash. Other solutions are focussed on retaining a local copy of the object at each JVM and transmitting all the changes to the object over a shared pub/sub channel to each other participant. It is obvious that the solution used for Redisson Live Objects is superior compared with the solutions from other projects. While other solutions have employed complicated mechanisms to guarantee each participant receives and reflects all the changes in same order, Redisson naturally inherits this guarantee from redis because it is a single-threaded centralised service. Commands sent to it are always processed in a first-come, first-served manner. Because the Redisson Live Object keeps its state in a remote repository, the state can be retained even after all of the participants go offline, whereas other solutions require at least one participant to stay online at any time to ensure the state of the objects are not lost.</p>
<h2>Usage</h2>
<p pid="22">In order to enjoy all the benefits brought by Redisson Live Object, only thing you need to do is annotate the Class you desire to use with @REntity, then annotate a field with @RId. </p>
<div class="CodeMirror cm-s-default"><div class="CodeMirror-scroll"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 30px; min-width: 319px; min-height: 26px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-code"><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="1"></div></div><pre><span style="padding-right: 29px;"><span class="cm-meta">@REntity</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="2"></div></div><pre><span style="padding-right: 29px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">MyLiveObject</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="3"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-meta">@RId</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="4"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">private</span> <span class="cm-type">String</span> <span class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="5"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-comment">//other fields</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="6"></div></div><pre><span style="padding-right: 29px;"> ...</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="7"></div></div><pre><span style="padding-right: 29px;"> ...</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="8"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="9"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-comment">//getters and setters</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="10"></div></div><pre><span style="padding-right: 29px;"> ...</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="11"></div></div><pre><span style="padding-right: 29px;"> ...</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="12"></div></div><pre><span style="padding-right: 29px;">}</span></pre></div></div></div></div></div></div><div class="CodeMirror-gutters" style="height: 232px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div>
<p pid="23">Now you have made an otherwise standard Java object class into a Redisson Live Object class. You are able to get an instance of it with the <code>RedissonLiveObjectService</code>, which you can get from a RedissonClient.</p>
<div class="CodeMirror cm-s-default"><div class="CodeMirror-scroll"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 30px; min-width: 319px; min-height: 26px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-code"><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="1"></div></div><pre><span style="padding-right: 29px;">...</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="2"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">RLiveObjectService</span> <span class="cm-variable">service</span> <span class="cm-operator">=</span> <span class="cm-variable">redisson</span>.<span class="cm-variable">getLiveObjectService</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="3"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">MyLiveObject</span> <span class="cm-variable">myObject1</span> <span class="cm-operator">=</span> <span class="cm-variable">service</span>.<span class="cm-operator"><</span><span class="cm-variable">MyLiveObject</span>, <span class="cm-type">String</span><span class="cm-operator">></span><span class="cm-def">getOrCreate</span>(<span class="cm-variable">MyLiveObject</span>.<span class="cm-keyword">class</span>, <span class="cm-string">"myObjectId"</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="4"></div></div><pre><span style="padding-right: 29px;">...</span></pre></div></div></div></div></div></div><div class="CodeMirror-gutters" style="height: 83px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div>
<p pid="26">Using the Redisson Live Object is the same as using a standard Java object. Let's assume you have this object:</p>
<div class="CodeMirror cm-s-default"><div class="CodeMirror-scroll"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 30px; min-width: 319px; min-height: 26px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-code"><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="1"></div></div><pre><span style="padding-right: 29px;"><span class="cm-meta">@REntity</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="2"></div></div><pre><span style="padding-right: 29px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">MyObject</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="3"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-meta">@RId</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="4"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">private</span> <span class="cm-type">String</span> <span class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="5"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">private</span> <span class="cm-type">String</span> <span class="cm-variable">value</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="6"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="7"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">public</span> <span class="cm-variable">MyObject</span>(<span class="cm-type">String</span> <span class="cm-variable">name</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="8"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">this</span>.<span class="cm-variable">name</span> <span class="cm-operator">=</span> <span class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="9"></div></div><pre><span style="padding-right: 29px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="10"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="11"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">public</span> <span class="cm-variable">MyObject</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="12"></div></div><pre><span style="padding-right: 29px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="13"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="14"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">public</span> <span class="cm-type">String</span> <span class="cm-variable">getName</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="15"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">return</span> <span class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="16"></div></div><pre><span style="padding-right: 29px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="17"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="18"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">public</span> <span class="cm-type">String</span> <span class="cm-variable">getValue</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="19"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">return</span> <span class="cm-variable">value</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="20"></div></div><pre><span style="padding-right: 29px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="21"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="22"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">public</span> <span class="cm-type">void</span> <span class="cm-variable">setName</span>(<span class="cm-type">String</span> <span class="cm-variable">name</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="23"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">this</span>.<span class="cm-variable">name</span> <span class="cm-operator">=</span> <span class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="24"></div></div><pre><span style="padding-right: 29px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="25"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="26"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">public</span> <span class="cm-type">void</span> <span class="cm-variable">setValue</span>(<span class="cm-type">String</span> <span class="cm-variable">value</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="27"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">this</span>.<span class="cm-variable">value</span> <span class="cm-operator">=</span> <span class="cm-variable">value</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="28"></div></div><pre><span style="padding-right: 29px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="29"></div></div><pre><span style="padding-right: 29px;">}</span></pre></div></div></div></div></div></div><div class="CodeMirror-gutters" style="height: 549px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div>
<p pid="27">Somewhere else in the code you may want to create it as a standard Java instance.</p>
<div class="CodeMirror cm-s-default"><div class="CodeMirror-scroll"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 30px; min-width: 319px; min-height: 26px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-code"><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="1"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//Standard Java object instance</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="2"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">MyObject</span> <span class="cm-variable">standardObject1</span> <span class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">MyObject</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="3"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">standardObject1</span>.<span class="cm-variable">setName</span>(<span class="cm-string">"standard1"</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="4"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="5"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//Of course you can use non-default constructor</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="6"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">MyObject</span> <span class="cm-variable">standardObject2</span> <span class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">MyObject</span>(<span class="cm-string">"standard2"</span>);</span></pre></div></div></div></div></div></div><div class="CodeMirror-gutters" style="height: 120px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div>
<p pid="28">Elsewhere, you may also want to create it as a Redisson Live Object instance:</p>
<div class="CodeMirror cm-s-default"><div class="CodeMirror-scroll"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 30px; min-width: 319px; min-height: 26px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-code"><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="1"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//first create the service</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="2"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">RLiveObjectService</span> <span class="cm-variable">service</span> <span class="cm-operator">=</span> <span class="cm-variable">redisson</span>.<span class="cm-variable">getLiveObjectService</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="3"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="4"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//instantiate the object with the service</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="5"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">MyObject</span> <span class="cm-variable">liveObject1</span> <span class="cm-operator">=</span> <span class="cm-variable">service</span>.<span class="cm-operator"><</span><span class="cm-variable">MyObject</span>, <span class="cm-type">String</span><span class="cm-operator">></span><span class="cm-def">getOrCreate</span>(<span class="cm-variable">MyObject</span>.<span class="cm-keyword">class</span>, <span class="cm-string">"liveObjectId"</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="6"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//Behind scense, it tries to locate the constructor with one argument and invoke with the id value, </span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="7"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//"liveObjectId" in this case. If the constructor is not found, falls back on default constructor</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="8"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//and then call setName("liveObjectId") before returns back to you.</span></span></pre></div></div></div></div></div></div><div class="CodeMirror-gutters" style="height: 157px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div>
<p pid="29">There is literally no difference when it comes to using these instances:</p>
<div class="CodeMirror cm-s-default"><div class="CodeMirror-scroll"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 30px; min-width: 319px; min-height: 26px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-code"><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="1"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//Setting the "value" field is the same</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="2"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">standardObject1</span>.<span class="cm-variable">setValue</span>(<span class="cm-string">"abc"</span>);<span class="cm-comment">//the value "abc" is stored in heapspace in VM</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="3"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="4"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">standardObject2</span>.<span class="cm-variable">setValue</span>(<span class="cm-string">"abc"</span>);<span class="cm-comment">//same as above</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="5"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="6"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">liveObject1</span>.<span class="cm-variable">setValue</span>(<span class="cm-string">"abc"</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="7"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//the value "abc" is stored inside redis, no value is stored in heap. (OK, there</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="8"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//is a string pool, but the value is not referenced here in the object, so it can</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="9"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//be garbage collected.)</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="10"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="11"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//Getting the "value" out is just the same</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="12"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">standardObject1</span>.<span class="cm-variable">getValue</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="13"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//It should give you "abc" in the console, the value is retrieved from heapspace in the VM;</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="14"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="15"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">standardObject2</span>.<span class="cm-variable">getValue</span>());<span class="cm-comment">//same as above.</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="16"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="17"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">liveObject1</span>.<span class="cm-variable">getValue</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="18"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//output is the same as above, but the value is retrieved from redis.</span></span></pre></div></div></div></div></div></div><div class="CodeMirror-gutters" style="height: 344px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div>
<p pid="30">While these two snippets of code look exactly the same, there is a slight difference between them. Let me explain it with another example:</p>
<div class="CodeMirror cm-s-default"><div class="CodeMirror-scroll"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 30px; min-width: 319px; min-height: 26px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-code"><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="1"></div></div><pre><span style="padding-right: 29px;"><span class="cm-meta">@REntity</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="2"></div></div><pre><span style="padding-right: 29px;"><span class="cm-keyword">public</span> <span class="cm-keyword">class</span> <span class="cm-def">MyLiveObject</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="3"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-meta">@RId</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="4"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">private</span> <span class="cm-type">String</span> <span class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="5"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">private</span> <span class="cm-variable">MyOtherObject</span> <span class="cm-variable">value</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="6"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="7"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">public</span> <span class="cm-variable">MyLiveObject</span>(<span class="cm-type">String</span> <span class="cm-variable">name</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="8"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">this</span>.<span class="cm-variable">name</span> <span class="cm-operator">=</span> <span class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="9"></div></div><pre><span style="padding-right: 29px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="10"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="11"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">public</span> <span class="cm-variable">MyObject</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="12"></div></div><pre><span style="padding-right: 29px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="13"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="14"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">public</span> <span class="cm-type">String</span> <span class="cm-variable">getName</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="15"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">return</span> <span class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="16"></div></div><pre><span style="padding-right: 29px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="17"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="18"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">public</span> <span class="cm-variable">MyOtherObject</span> <span class="cm-variable">getValue</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="19"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">return</span> <span class="cm-variable">value</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="20"></div></div><pre><span style="padding-right: 29px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="21"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="22"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">public</span> <span class="cm-type">void</span> <span class="cm-variable">setName</span>(<span class="cm-type">String</span> <span class="cm-variable">name</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="23"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">this</span>.<span class="cm-variable">name</span> <span class="cm-operator">=</span> <span class="cm-variable">name</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="24"></div></div><pre><span style="padding-right: 29px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="25"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="26"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">public</span> <span class="cm-type">void</span> <span class="cm-variable">setValue</span>(<span class="cm-variable">MyOtherObject</span> <span class="cm-variable">value</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="27"></div></div><pre><span style="padding-right: 29px;"> <span class="cm-keyword">this</span>.<span class="cm-variable">value</span> <span class="cm-operator">=</span> <span class="cm-variable">value</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="28"></div></div><pre><span style="padding-right: 29px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="29"></div></div><pre><span style="padding-right: 29px;">}</span></pre></div></div></div></div></div></div><div class="CodeMirror-gutters" style="height: 549px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div>
<p pid="31">In this case, the type of the "value" field is a mutable type. In a standard Java object, when you invoke the <code>getValue()</code>method, the reference to this <code>MyOtherObject</code> instance is returned to you. When you invoke the same method on a Redisson Live Object, a reference of a new instance is returned. This can have the following two effects:</p>
<div class="CodeMirror cm-s-default"><div class="CodeMirror-scroll"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 30px; min-width: 319px; min-height: 26px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-code"><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="1"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//Redisson Live Object behaviour:</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="2"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">MyLiveObject</span> <span class="cm-variable">myLiveObject</span> <span class="cm-operator">=</span> <span class="cm-variable">service</span>.<span class="cm-variable">getOrCreate</span>(<span class="cm-variable">MyLiveObject</span>.<span class="cm-keyword">class</span>, <span class="cm-string">"1"</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="3"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">myLiveObject</span>.<span class="cm-variable">setValue</span>(<span class="cm-keyword">new</span> <span class="cm-variable">MyOtherObject</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="4"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">myLiveObject</span>.<span class="cm-variable">getValue</span>() <span class="cm-operator">==</span> <span class="cm-variable">myLiveObject</span>.<span class="cm-variable">getValue</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="5"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//False (unless you use a custom Codec with object pooling)</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="6"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="7"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//Standard Java Object behaviour:</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="8"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">MyLiveObject</span> <span class="cm-variable">notLiveObject</span> <span class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">MyLiveObject</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="9"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">notLiveObject</span>.<span class="cm-variable">setValue</span>(<span class="cm-keyword">new</span> <span class="cm-variable">MyOtherObject</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="10"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">notLiveObject</span>.<span class="cm-variable">getValue</span>() <span class="cm-operator">==</span> <span class="cm-variable">notLiveObject</span>.<span class="cm-variable">getValue</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="11"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//True</span></span></pre></div></div></div></div></div></div><div class="CodeMirror-gutters" style="height: 213px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div>
<div class="CodeMirror cm-s-default"><div class="CodeMirror-scroll"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 30px; min-width: 319px; min-height: 26px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-code"><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="1"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//Redisson Live Object behaviour:</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="2"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">MyLiveObject</span> <span class="cm-variable">myLiveObject</span> <span class="cm-operator">=</span> <span class="cm-variable">service</span>.<span class="cm-variable">getOrCreate</span>(<span class="cm-variable">MyLiveObject</span>.<span class="cm-keyword">class</span>, <span class="cm-string">"1"</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="3"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">MyOtherObject</span> <span class="cm-variable">other</span> <span class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">MyOtherObject</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="4"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">other</span>.<span class="cm-variable">setOtherName</span>(<span class="cm-string">"ABC"</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="5"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">myLiveObject</span>.<span class="cm-variable">setValue</span>(<span class="cm-variable">other</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="6"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">myLiveObject</span>.<span class="cm-variable">getValue</span>().<span class="cm-variable">getOtherName</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="7"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//ABC</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="8"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="9"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">other</span>.<span class="cm-variable">setOtherName</span>(<span class="cm-string">"BCD"</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="10"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">myLiveObject</span>.<span class="cm-variable">getValue</span>().<span class="cm-variable">getOtherName</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="11"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//still ABC</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="12"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="13"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">myLiveObject</span>.<span class="cm-variable">setValue</span>(<span class="cm-variable">other</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="14"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">myLiveObject</span>.<span class="cm-variable">getValue</span>().<span class="cm-variable">getOtherName</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="15"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//now it's BCD</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="16"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="17"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="18"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//Standard Java Object behaviour:</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="19"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">MyLiveObject</span> <span class="cm-variable">myLiveObject</span> <span class="cm-operator">=</span> <span class="cm-variable">service</span>.<span class="cm-variable">getOrCreate</span>(<span class="cm-variable">MyLiveObject</span>.<span class="cm-keyword">class</span>, <span class="cm-string">"1"</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="20"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">MyOtherObject</span> <span class="cm-variable">other</span> <span class="cm-operator">=</span> <span class="cm-keyword">new</span> <span class="cm-variable">MyOtherObject</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="21"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">other</span>.<span class="cm-variable">setOtherName</span>(<span class="cm-string">"ABC"</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="22"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">myLiveObject</span>.<span class="cm-variable">setValue</span>(<span class="cm-variable">other</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="23"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">myLiveObject</span>.<span class="cm-variable">getValue</span>().<span class="cm-variable">getOtherName</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="24"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//ABC</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="25"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="26"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">other</span>.<span class="cm-variable">setOtherName</span>(<span class="cm-string">"BCD"</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="27"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">myLiveObject</span>.<span class="cm-variable">getValue</span>().<span class="cm-variable">getOtherName</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="28"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//already is BCD</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="29"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="30"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">myLiveObject</span>.<span class="cm-variable">setValue</span>(<span class="cm-variable">other</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="31"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">myLiveObject</span>.<span class="cm-variable">getValue</span>().<span class="cm-variable">getOtherName</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="32"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//still is BCD</span></span></pre></div></div></div></div></div></div><div class="CodeMirror-gutters" style="height: 605px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div>
<p pid="32">The reason for this difference in behavior is because we are not keeping any of the object states, and each setter and getter call will serialize and deserialize the value to and from Redis back to a local VM. This effectively detaches the field value from the object state. This behavior is usually not a problem when the value type is an immutable type, such as String, Double, Long, etc. When you are dealing with a mutable type, you may want to benefit from this behaviour, since the value instance is detached off from the object state, you can consider all the read/write actions to this value instance is effectively in a transaction with <a href="https://en.wikipedia.org/wiki/ACID" rel="nofollow">ACID</a> property. It can be extremely useful when the application is designed to incorporate this behavior properly. If you prefer to stick to standard Java behavior, you can always convert the <code>MyOtherObject</code> into a Redisson Live Object. </p>
<div class="CodeMirror cm-s-default"><div class="CodeMirror-scroll"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 30px; min-width: 319px; min-height: 26px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-code"><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="1"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//Redisson Live Object with nested Redisson Live Object behaviour:</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="2"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">MyLiveObject</span> <span class="cm-variable">myLiveObject</span> <span class="cm-operator">=</span> <span class="cm-variable">service</span>.<span class="cm-variable">getOrCreate</span>(<span class="cm-variable">MyLiveObject</span>.<span class="cm-keyword">class</span>, <span class="cm-string">"1"</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="3"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">MyOtherObject</span> <span class="cm-variable">other</span> <span class="cm-operator">=</span> <span class="cm-variable">service</span>.<span class="cm-variable">getOrCreate</span>(<span class="cm-variable">MyOtherObject</span>.<span class="cm-keyword">class</span>, <span class="cm-string">"2"</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="4"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">other</span>.<span class="cm-variable">setOtherName</span>(<span class="cm-string">"ABC"</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="5"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">myLiveObject</span>.<span class="cm-variable">setValue</span>(<span class="cm-variable">other</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="6"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">myLiveObject</span>.<span class="cm-variable">getValue</span>().<span class="cm-variable">getOtherName</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="7"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//ABC</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="8"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="9"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">other</span>.<span class="cm-variable">setOtherName</span>(<span class="cm-string">"BCD"</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="10"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">myLiveObject</span>.<span class="cm-variable">getValue</span>().<span class="cm-variable">getOtherName</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="11"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//you see, already is BCD </span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="12"></div></div><pre><span style="padding-right: 29px;"></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="13"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">myLiveObject</span>.<span class="cm-variable">setValue</span>(<span class="cm-variable">other</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="14"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">System</span>.<span class="cm-variable">out</span>.<span class="cm-variable">println</span>(<span class="cm-variable">myLiveObject</span>.<span class="cm-variable">getValue</span>().<span class="cm-variable">getOtherName</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="15"></div></div><pre><span style="padding-right: 29px;"><span class="cm-comment">//and again still is BCD</span></span></pre></div></div></div></div></div></div><div class="CodeMirror-gutters" style="height: 288px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div>
<p pid="33">Field types in the Redisson Live Object can be almost anything, from Java util classes to collection/map types and of course your own custom objects, as long as it can be encoded and decoded by a supplied codec. More details about the codec can be found in the <strong><em>Advanced Usage</em></strong> section. </p>
<p pid="34">As much as I like to say it's free with no limits, there are still some restrictions on the choices of field types you can have. The field annotated with <code>RId</code> can not be an array type, i.e. int[], long[], double[], byte[], etc. More details and explainations can be found in <strong><em>Restrictions</em></strong> section</p>
<p pid="35">In order to keep Redisson Live Objects behaving as closely to standard Java objects as possible, Redisson automatically converts the following standard Java field types to its counter types supported by Redisson <code>RObject</code>. </p>
<table>
<thead>
<tr>
<th>Standard Java Class</th>
<th>Converted Redisson Class</th>
</tr>
</thead>
<tbody>
<tr>
<td>SortedSet.class</td>
<td>RedissonSortedSet.class</td>
</tr>
<tr>
<td>Set.class</td>
<td>RedissonSet.class</td>
</tr>
<tr>
<td>ConcurrentMap.class</td>
<td>RedissonMap.class</td>
</tr>
<tr>
<td>Map.class</td>
<td>RedissonMap.class</td>
</tr>
<tr>
<td>BlockingDeque.class</td>
<td>RedissonBlockingDeque.class</td>
</tr>
<tr>
<td>Deque.class</td>
<td>RedissonDeque.class</td>
</tr>
<tr>
<td>BlockingQueue.class</td>
<td>RedissonBlockingQueue.class</td>
</tr>
<tr>
<td>Queue.class</td>
<td>RedissonQueue.class</td>
</tr>
<tr>
<td>List.class</td>
<td>RedissonList.class</td>
</tr>
</tbody>
</table>
<p pid="36">The conversion prefers the one nearer to the top of the table if a field type matches more than one entries. i.e. <code>LinkedList</code> implements <code>Deque</code>, <code>List</code>, <code>Queue</code>, it will be converted to a <code>RedissonDeque</code> because of this.</p>
<p pid="37">Instances of these Redisson classes retains their states, values, and entries in Redis too, changes to them are directly reflected into Redis without keeping values in local VM.</p>
<h2>Advanced Usage</h2>
<p pid="38">As described before, Redisson Live Object classes are proxy classes which can be fabricated when needed and then get cached in a <code>RedissonClient</code> instance against its original class. This process can be a bit slow and it is recommended to pre-register all the Redisson Live Object classes via <code>RedissonLiveObjectService</code> for any kind of delay-sensitive applications. The service can also be used to unregister a class if it is no longer needed. And of course it can be used to check if the class has already been registered.</p>
<div class="CodeMirror cm-s-default"><div class="CodeMirror-scroll"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 30px; min-width: 319px; min-height: 26px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-code"><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="1"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">RLiveObjectService</span> <span class="cm-variable">service</span> <span class="cm-operator">=</span> <span class="cm-variable">redisson</span>.<span class="cm-variable">getLiveObjectService</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="2"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">service</span>.<span class="cm-variable">registerClass</span>(<span class="cm-variable">MyClass</span>.<span class="cm-keyword">class</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="3"></div></div><pre><span style="padding-right: 29px;"><span class="cm-variable">service</span>.<span class="cm-variable">unregisterClass</span>(<span class="cm-variable">MyClass</span>.<span class="cm-keyword">class</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px; width: 29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;" data-line-number="4"></div></div><pre><span style="padding-right: 29px;"><span class="cm-type">Boolean</span> <span class="cm-variable">registered</span> <span class="cm-operator">=</span> <span class="cm-variable">service</span>.<span class="cm-variable">isClassRegistered</span>(<span class="cm-variable">MyClass</span>.<span class="cm-keyword">class</span>);</span></pre></div></div></div></div></div></div><div class="CodeMirror-gutters" style="height: 83px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div>
<h4><code>@REntity</code></h4>
<p pid="39">The behaviour of each type of Redisson Live Object can be customised through properties of the <code>@REntity</code> annotation. You can specify each of those properties to gain fine control over its behaviour.</p>
<ul>
<li>namingScheme - You can specify a naming scheme which tells Redisson how to assign key names for each instance of this class. It is used to create a reference to an existing Redisson Live Object and materialising a new one in redis. It defaults to use Redisson provided <code>DefaultNamingScheme</code>.</li>
<li>codec - You can tell Redisson which <code>Codec</code> class you want to use for the Redisson Live Object. Redisson will use an instance pool to locate the instance based on the class type. It defaults to <code>JsonJacksonCodec</code> provided by Redisson.</li>
<li>fieldTransformation - You can also specify a field transformation mode for the Redisson Live Object. As mentioned before, in order to keep everything as close to standard Java as possible, Redisson will automatically transform fields with commonly-used Java util classes to Redisson compatible classes. This uses <code>ANNOTATION_BASED</code> as the default value. You can set it to <code>IMPLEMENTATION_BASED</code> which will skip the transformation.</li>
</ul>
<h4><code>@RId</code></h4>
<p pid="40">The <code>@RId</code> annotation is used on a field that can be used to distinguish between one instance and another. Think of this field as the primary key field of this class. The value of this field is used to create a reference to existing Redisson Live Object. The field with this annotation is the only field that has its value also kept in the local VM. You can only have one <code>RId</code> annotation per class.</p>
<p pid="41">You can supply a <code>generator</code> strategy to the <code>@RId</code> annotation if you want the value of this field to be programatically generated. The default generator is <code>RandomUUIDIdStringGenerator</code> which generates a v4(Random) UUID string when used.</p>
<h4><code>@RObjectField</code></h4>
<p pid="42">When the <code>transformationMode</code> in <code>@REntity</code> is set to <code>ANNOTATION_BASED</code>, which is the default value, you can optionally use it to annotate a field that does not have <code>@RId</code> annotation at the same time. This is often used to give a different <code>namingScheme</code> and/or a different <code>codec</code> class to the ones specified in <code>@REntity</code>. </p>
<p pid="43">As you can see the <code>codec</code> and <code>namingScheme</code> are quite often used in providing a Redisson Live Object and its service, in order to reduce the amount of redundant instances. Redisson, by default, caches these instances internally to be reused. You can supply your own providers for each of them via Redisson's <code>Config</code> instance.</p>
<h2>Restrictions</h2>
<p pid="44">At the moment, Redisson Live Objects can only be classes with the default constructor or classes with a single-argument constructor, and the argument is assumed to be used as the value for field with <code>RId</code> annotaion. As mentioned above, the type of the <code>RId</code> field cannot be an Array type. This is due to the <code>DefaultNamingScheme</code> which cannot serialize and deserialize the Array type as of yet. This restriction can be lifted once the <code>DefaultNamingScheme</code> is improved. Since the <code>RId</code> field is encoded as part of the key name used by the underlying RMap, it makes no sense to create a RLO with just have one field. It is better to use a <code>RBucket</code> for this type of usage.</p>

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,884评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,755评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,369评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,799评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,910评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,096评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,159评论 3 411
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,917评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,360评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,673评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,814评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,509评论 4 334
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,156评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,882评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,123评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,641评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,728评论 2 351

推荐阅读更多精彩内容

  • 2016-12-1 晴 2013年底,我在公司负责文案,说是负责,其实就是赶鸭子上架,没人可用,看我还行,就把我...
    实干作品阅读 270评论 0 0
  • 1.巫婆晨读打卡 苏东坡说“余旧过陈州,留七十馀日,近城可观者无不至。”苏东坡一生,虽是屡遭贬谪,甚至一月三谪,贬...
    王路晨读晚课读者群3阅读 264评论 0 1
  • 阅读书名《你的生命有什么可能》 阅读有效时间:65分 阅读困难:一直忧虑兴趣修炼不易 阅读收获:我一直觉得自己没有...
    玉华_219a阅读 212评论 0 0
  • 烟在指间亮了又灭,灭了又亮。 烟灰缸里已戳满了烟蒂,他不知道自己到底抽了多少根,只知道嘴巴里满是水泡,干燥苦涩。 ...
    放牛冰寒阅读 384评论 2 3
  • 从相识到分开,一个月多几天,有些道理平时都懂,但是就是在自己遇见的时候选择性忽略,比如一个人是否真的重视你,是否真...
    梅开瑞阅读 122评论 0 0