IGNITE-45 - Examples

Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/280b4f68
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/280b4f68
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/280b4f68

Branch: refs/heads/ignite-45
Commit: 280b4f68d6421f32b952d62cf1ffbe6b22fcd08a
Parents: 8568c70
Author: Valentin Kulichenko <vkuliche...@gridgain.com>
Authored: Sun Mar 22 17:03:14 2015 -0700
Committer: Valentin Kulichenko <vkuliche...@gridgain.com>
Committed: Sun Mar 22 17:03:14 2015 -0700

----------------------------------------------------------------------
 .../hibernate/example-hibernate-L2-cache.xml    |   3 -
 .../hibernate/HibernateL2CacheExample.java      | 135 ++++++++++++-------
 .../HibernateL2CacheExampleNodeStartup.java     |  97 -------------
 ...ibernateL2CacheExampleMultiNodeSelfTest.java |   2 +-
 .../cache/hibernate/HibernateRegionFactory.java |  15 +--
 5 files changed, 91 insertions(+), 161 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/280b4f68/examples/config/hibernate/example-hibernate-L2-cache.xml
----------------------------------------------------------------------
diff --git a/examples/config/hibernate/example-hibernate-L2-cache.xml 
b/examples/config/hibernate/example-hibernate-L2-cache.xml
index 324c040..3248946 100644
--- a/examples/config/hibernate/example-hibernate-L2-cache.xml
+++ b/examples/config/hibernate/example-hibernate-L2-cache.xml
@@ -46,9 +46,6 @@
         <!-- Specify Ignite as L2 cache provider. -->
         <property 
name="cache.region.factory_class">org.apache.ignite.cache.hibernate.HibernateRegionFactory</property>
 
-        <!-- Specify the name of the ignite, that will be used for second 
level caching. -->
-        <property 
name="org.apache.ignite.hibernate.grid_name">hibernate-grid</property>
-
         <!-- Specify connection release mode. -->
         <property name="connection.release_mode">on_close</property>
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/280b4f68/examples/src/main/java/org/apache/ignite/examples/datagrid/hibernate/HibernateL2CacheExample.java
----------------------------------------------------------------------
diff --git 
a/examples/src/main/java/org/apache/ignite/examples/datagrid/hibernate/HibernateL2CacheExample.java
 
b/examples/src/main/java/org/apache/ignite/examples/datagrid/hibernate/HibernateL2CacheExample.java
index 7ba7ea1..6bb3f33 100644
--- 
a/examples/src/main/java/org/apache/ignite/examples/datagrid/hibernate/HibernateL2CacheExample.java
+++ 
b/examples/src/main/java/org/apache/ignite/examples/datagrid/hibernate/HibernateL2CacheExample.java
@@ -18,6 +18,8 @@
 package org.apache.ignite.examples.datagrid.hibernate;
 
 import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.apache.ignite.configuration.*;
 import org.apache.ignite.examples.*;
 import org.hibernate.*;
 import org.hibernate.cache.spi.access.AccessType;
@@ -28,6 +30,10 @@ import org.hibernate.stat.*;
 import java.net.*;
 import java.util.*;
 
+import static org.apache.ignite.cache.CacheAtomicityMode.*;
+import static org.apache.ignite.cache.CacheMode.*;
+import static org.apache.ignite.cache.CacheWriteSynchronizationMode.*;
+
 /**
  * This example demonstrates the use of Ignite In-Memory Data Ignite cluster 
as a Hibernate
  * Second-Level cache provider.
@@ -63,7 +69,11 @@ import java.util.*;
  * can experiment with other access types by modifying the Hibernate 
configuration file
  * {@code 
IGNITE_HOME/examples/config/hibernate/example-hibernate-L2-cache.xml}, used by 
the example.
  * <p>
- * Remote nodes should always be started using {@link 
HibernateL2CacheExampleNodeStartup}
+ * Remote nodes should always be started with special configuration file which
+ * enables P2P class loading: {@code 'ignite.{sh|bat} 
examples/config/example-ignite.xml'}.
+ * <p>
+ * Alternatively you can run {@link ExampleNodeStartup} in another JVM which 
will
+ * start node with {@code examples/config/example-ignite.xml} configuration.
  */
 public class HibernateL2CacheExample {
     /** JDBC URL for backing database (an H2 in-memory database is used). */
@@ -84,84 +94,111 @@ public class HibernateL2CacheExample {
      */
     public static void main(String[] args) throws IgniteException {
         // Start the node, run the example, and stop the node when finished.
-        try (Ignite ignite = 
Ignition.start(HibernateL2CacheExampleNodeStartup.configuration())) {
+        try (Ignite ignite = 
Ignition.start("examples/config/example-ignite.xml")) {
             // We use a single session factory, but create a dedicated session
             // for each transaction or query. This way we ensure that L1 cache
             // is not used (L1 cache has per-session scope only).
             System.out.println();
             System.out.println(">>> Hibernate L2 cache example started.");
 
-            URL hibernateCfg = ExamplesUtils.url(HIBERNATE_CFG);
+            try (
+                // Create all required caches.
+                IgniteCache c1 = 
createCache("org.hibernate.cache.spi.UpdateTimestampsCache", ATOMIC);
+                IgniteCache c2 = 
createCache("org.hibernate.cache.internal.StandardQueryCache", ATOMIC);
+                IgniteCache c3 = 
createCache("org.apache.ignite.examples.datagrid.hibernate.User", 
TRANSACTIONAL);
+                IgniteCache c4 = 
createCache("org.apache.ignite.examples.datagrid.hibernate.User.posts", 
TRANSACTIONAL);
+                IgniteCache c5 = 
createCache("org.apache.ignite.examples.datagrid.hibernate.Post", TRANSACTIONAL)
+            ) {
+                URL hibernateCfg = ExamplesUtils.url(HIBERNATE_CFG);
 
-            SessionFactory sesFactory = 
createHibernateSessionFactory(hibernateCfg);
+                SessionFactory sesFactory = 
createHibernateSessionFactory(hibernateCfg);
 
-            System.out.println();
-            System.out.println(">>> Creating objects.");
+                System.out.println();
+                System.out.println(">>> Creating objects.");
 
-            final long userId;
+                final long userId;
 
-            Session ses = sesFactory.openSession();
+                Session ses = sesFactory.openSession();
 
-            try {
-                Transaction tx = ses.beginTransaction();
+                try {
+                    Transaction tx = ses.beginTransaction();
 
-                User user = new User("jedi", "Luke", "Skywalker");
+                    User user = new User("jedi", "Luke", "Skywalker");
 
-                user.getPosts().add(new Post(user, "Let the Force be with 
you."));
+                    user.getPosts().add(new Post(user, "Let the Force be with 
you."));
 
-                ses.save(user);
+                    ses.save(user);
 
-                tx.commit();
+                    tx.commit();
 
-                // Create a user object, store it in DB, and save the 
database-generated
-                // object ID. You may try adding more objects in a similar way.
-                userId = user.getId();
-            }
-            finally {
-                ses.close();
-            }
+                    // Create a user object, store it in DB, and save the 
database-generated
+                    // object ID. You may try adding more objects in a similar 
way.
+                    userId = user.getId();
+                }
+                finally {
+                    ses.close();
+                }
 
-            // Output L2 cache and Ignite cache stats. You may notice that
-            // at this point the object is not yet stored in L2 cache, because
-            // the read was not yet performed.
-            printStats(sesFactory);
+                // Output L2 cache and Ignite cache stats. You may notice that
+                // at this point the object is not yet stored in L2 cache, 
because
+                // the read was not yet performed.
+                printStats(sesFactory);
 
-            System.out.println();
-            System.out.println(">>> Querying object by ID.");
+                System.out.println();
+                System.out.println(">>> Querying object by ID.");
 
-            // Query user by ID several times. First time we get an L2 cache
-            // miss, and the data is queried from DB, but it is then stored
-            // in cache and successive queries hit the cache and return
-            // immediately, no SQL query is made.
-            for (int i = 0; i < 3; i++) {
-                ses = sesFactory.openSession();
+                // Query user by ID several times. First time we get an L2 
cache
+                // miss, and the data is queried from DB, but it is then stored
+                // in cache and successive queries hit the cache and return
+                // immediately, no SQL query is made.
+                for (int i = 0; i < 3; i++) {
+                    ses = sesFactory.openSession();
 
-                try {
-                    Transaction tx = ses.beginTransaction();
+                    try {
+                        Transaction tx = ses.beginTransaction();
 
-                    User user = (User)ses.get(User.class, userId);
+                        User user = (User)ses.get(User.class, userId);
 
-                    System.out.println("User: " + user);
+                        System.out.println("User: " + user);
 
-                    for (Post post : user.getPosts())
-                        System.out.println("\tPost: " + post);
+                        for (Post post : user.getPosts())
+                            System.out.println("\tPost: " + post);
 
-                    tx.commit();
+                        tx.commit();
+                    }
+                    finally {
+                        ses.close();
+                    }
                 }
-                finally {
-                    ses.close();
-                }
-            }
 
-            // Output the stats. We should see 1 miss and 2 hits for
-            // User and Collection object (stored separately in L2 cache).
-            // The Post is loaded with the collection, so it won't imply
-            // a miss.
-            printStats(sesFactory);
+                // Output the stats. We should see 1 miss and 2 hits for
+                // User and Collection object (stored separately in L2 cache).
+                // The Post is loaded with the collection, so it won't imply
+                // a miss.
+                printStats(sesFactory);
+            }
         }
     }
 
     /**
+     * Creates cache.
+     *
+     * @param name Cache name.
+     * @param atomicityMode Atomicity mode.
+     * @return Cache configuration.
+     */
+    private static IgniteCache createCache(String name, CacheAtomicityMode 
atomicityMode) {
+        CacheConfiguration ccfg = new CacheConfiguration();
+
+        ccfg.setName(name);
+        ccfg.setCacheMode(PARTITIONED);
+        ccfg.setAtomicityMode(atomicityMode);
+        ccfg.setWriteSynchronizationMode(FULL_SYNC);
+
+        return Ignition.ignite().createCache(ccfg);
+    }
+
+    /**
      * Creates a new Hibernate {@link SessionFactory} using a programmatic
      * configuration.
      *

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/280b4f68/examples/src/main/java/org/apache/ignite/examples/datagrid/hibernate/HibernateL2CacheExampleNodeStartup.java
----------------------------------------------------------------------
diff --git 
a/examples/src/main/java/org/apache/ignite/examples/datagrid/hibernate/HibernateL2CacheExampleNodeStartup.java
 
b/examples/src/main/java/org/apache/ignite/examples/datagrid/hibernate/HibernateL2CacheExampleNodeStartup.java
deleted file mode 100644
index cef6526..0000000
--- 
a/examples/src/main/java/org/apache/ignite/examples/datagrid/hibernate/HibernateL2CacheExampleNodeStartup.java
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ignite.examples.datagrid.hibernate;
-
-import org.apache.ignite.*;
-import org.apache.ignite.cache.*;
-import org.apache.ignite.configuration.*;
-import org.apache.ignite.spi.discovery.tcp.*;
-import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*;
-
-import java.util.*;
-
-import static org.apache.ignite.cache.CacheAtomicityMode.*;
-import static org.apache.ignite.cache.CacheMode.*;
-import static org.apache.ignite.cache.CacheWriteSynchronizationMode.*;
-
-/**
- * Starts up an empty node with example cache configuration.
- */
-public class HibernateL2CacheExampleNodeStartup {
-    /**
-     * Start up an empty node with specified cache configuration.
-     *
-     * @param args Command line arguments, none required.
-     * @throws IgniteException If example execution failed.
-     */
-    public static void main(String[] args) throws IgniteException {
-        Ignition.start(configuration());
-    }
-
-    /**
-     * Create Ignite configuration with IGFS and enabled IPC.
-     *
-     * @return Ignite configuration.
-     * @throws IgniteException If configuration creation failed.
-     */
-    public static IgniteConfiguration configuration() throws IgniteException {
-        IgniteConfiguration cfg = new IgniteConfiguration();
-
-        cfg.setGridName("hibernate-grid");
-        cfg.setLocalHost("127.0.0.1");
-        cfg.setConnectorConfiguration(null);
-
-        TcpDiscoverySpi discoSpi = new TcpDiscoverySpi();
-
-        TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
-
-        
ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));
-
-        discoSpi.setIpFinder(ipFinder);
-
-        cfg.setDiscoverySpi(discoSpi);
-
-        cfg.setCacheConfiguration(
-            
cacheConfiguration("org.hibernate.cache.spi.UpdateTimestampsCache", ATOMIC),
-            
cacheConfiguration("org.hibernate.cache.internal.StandardQueryCache", ATOMIC),
-            
cacheConfiguration("org.apache.ignite.examples.datagrid.hibernate.User", 
TRANSACTIONAL),
-            
cacheConfiguration("org.apache.ignite.examples.datagrid.hibernate.User.posts", 
TRANSACTIONAL),
-            
cacheConfiguration("org.apache.ignite.examples.datagrid.hibernate.Post", 
TRANSACTIONAL)
-        );
-
-        return cfg;
-    }
-
-    /**
-     * Create cache configuration.
-     *
-     * @param name Cache name.
-     * @param atomicityMode Atomicity mode.
-     * @return Cache configuration.
-     */
-    private static CacheConfiguration cacheConfiguration(String name, 
CacheAtomicityMode atomicityMode) {
-        CacheConfiguration ccfg = new CacheConfiguration();
-
-        ccfg.setName(name);
-        ccfg.setCacheMode(PARTITIONED);
-        ccfg.setAtomicityMode(atomicityMode);
-        ccfg.setWriteSynchronizationMode(FULL_SYNC);
-
-        return ccfg;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/280b4f68/examples/src/test/java/org/apache/ignite/examples/HibernateL2CacheExampleMultiNodeSelfTest.java
----------------------------------------------------------------------
diff --git 
a/examples/src/test/java/org/apache/ignite/examples/HibernateL2CacheExampleMultiNodeSelfTest.java
 
b/examples/src/test/java/org/apache/ignite/examples/HibernateL2CacheExampleMultiNodeSelfTest.java
index 29a85b8..ea14374 100644
--- 
a/examples/src/test/java/org/apache/ignite/examples/HibernateL2CacheExampleMultiNodeSelfTest.java
+++ 
b/examples/src/test/java/org/apache/ignite/examples/HibernateL2CacheExampleMultiNodeSelfTest.java
@@ -26,6 +26,6 @@ public class HibernateL2CacheExampleMultiNodeSelfTest extends 
HibernateL2CacheEx
     /** {@inheritDoc} */
     @Override protected void beforeTest() throws Exception {
         for (int i = 0; i < RMT_NODES_CNT; i++)
-            startGrid("node-" + i, 
HibernateL2CacheExampleNodeStartup.configuration());
+            startGrid("node-" + i, "examples/config/example-ignite.xml");
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/280b4f68/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/HibernateRegionFactory.java
----------------------------------------------------------------------
diff --git 
a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/HibernateRegionFactory.java
 
b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/HibernateRegionFactory.java
index 55abc59..478565e 100644
--- 
a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/HibernateRegionFactory.java
+++ 
b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/HibernateRegionFactory.java
@@ -92,16 +92,10 @@ public class HibernateRegionFactory implements 
RegionFactory {
 
     /** {@inheritDoc} */
     @Override public void start(Settings settings, Properties props) throws 
CacheException {
+        String gridCfg = props.getProperty(GRID_CONFIG_PROPERTY);
         String gridName = props.getProperty(GRID_NAME_PROPERTY);
 
-        if (gridName != null)
-            ignite = G.ignite(gridName);
-        else {
-            String gridCfg = props.getProperty(GRID_CONFIG_PROPERTY);
-
-            if (gridCfg == null)
-                throw new CacheException("Either grid name or path to grid 
configuration must be specified.");
-
+        if (gridCfg != null) {
             try {
                 ignite = G.start(gridCfg);
             }
@@ -109,9 +103,8 @@ public class HibernateRegionFactory implements 
RegionFactory {
                 throw new CacheException(e);
             }
         }
-
-        if (ignite == null)
-            throw new CacheException("Grid '" + gridName + "' for hibernate L2 
cache is not started.");
+        else
+            ignite = Ignition.ignite(gridName);
 
         String accessType = props.getProperty(DFLT_ACCESS_TYPE_PROPERTY, 
NONSTRICT_READ_WRITE.name());
 

Reply via email to