2015-10-17 268 views
0

这个类实现MapStoreHazelcast缓存数据库数据缓存的Java

package jdbc; 
import com.hazelcast.core.MapStore; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.Collection; 
import java.util.HashMap; 
import java.util.Map; 

import static java.lang.String.format; 
import data.Person; 

public class PersonMap implements MapStore<Long, Person> { 

    private final Connection con; 
    private PreparedStatement allKeysStatement; 

    public PersonMap() throws ClassNotFoundException { 
     try { 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "hr"); 
      /* con.createStatement().executeUpdate(
        "create table if not exists person (id bigint not null, name varchar(45), primary key (id))");*/ 
      allKeysStatement = con.prepareStatement("select * from person"); 
     } catch (SQLException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public synchronized void delete(Long key) { 
     System.out.println("Delete:" + key); 
     try { 
      con.createStatement().executeUpdate(
        format("delete from person where id = %s", key)); 
     } catch (SQLException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public synchronized void store(Long key, Person value) { 
     try { 
      con.createStatement().executeUpdate(
        format("insert into person values(%s,'%s')", key, value.name)); 
     } catch (SQLException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public synchronized void storeAll(Map<Long, Person> map) { 
     for (Map.Entry<Long, Person> entry : map.entrySet()) 
      store(entry.getKey(), entry.getValue()); 
    } 

    public synchronized void deleteAll(Collection<Long> keys) { 
     for (Long key : keys) delete(key); 
    } 

    public synchronized Person load(Long key) { 
     try { 
      ResultSet resultSet = con.createStatement().executeQuery(
        format("select name from person where id =%s", key)); 
      try { 
       if (!resultSet.next()) return null; 
       String name = resultSet.getString(1); 
       return new Person(key, name); 
      } finally { 
       resultSet.close(); 
      } 
     } catch (SQLException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public synchronized Map<Long, Person> loadAll(Collection<Long> keys) { 
     Map<Long, Person> result = new HashMap<Long, Person>(); 
     for (Long key : keys) result.put(key, load(key)); 
     return result; 
    } 

    public Iterable<Long> loadAllKeys() { 
     return new StatementIterable<Long>(allKeysStatement); 
    } 

} 

这是人类 包数据; import java.io.Serializable;

public class Person implements Serializable { 

    public Long id; 
    public String name; 

    public Person() { 
    } 

    public Person(Long id, String name) { 
     this.id = id; 
     this.name = name; 
    } 

    public String toString() { 
     return "Person{name='" + name + "'}"; 
    } 
} 

这个主要的方法类,但我不能在实例中加载数据。

import com.hazelcast.core.*; 
import com.hazelcast.config.*; 
import com.hazelcast.config.MapStoreConfig.InitialLoadMode; 

import data.Person; 

import java.util.Map; 
import java.util.Queue; 

import jdbc.PersonMap; 

public class GettingStarted { 
    public static void main(String[] args) throws ClassNotFoundException { 
     /* Config cfg = new Config(); 
     HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg); 
     Map<Integer, String> mapCustomers = instance.getMap("customers"); 
     mapCustomers.put(1, "Joe"); 
     mapCustomers.put(2, "Ali"); 
     mapCustomers.put(3, "Avi"); 
     HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(cfg); 
     Map<Integer, String> mapCustomers1 = instance.getMap("customers"); 
     mapCustomers1.get(1); 

     System.out.println("Customer with key 1: "+ mapCustomers1.get(1)); 
     System.out.println("Map Size:" + mapCustomers1.size()); 

     Queue<String> queueCustomers = instance.getQueue("customers"); 
     queueCustomers.offer("Tom"); 
     queueCustomers.offer("Mary"); 
     queueCustomers.offer("Jane"); 
     System.out.println("First customer: " + queueCustomers.poll()); 
     System.out.println("Second customer: "+ queueCustomers.peek()); 
     System.out.println("Queue size: " + queueCustomers.size());*/ 
     Config config = new Config(); 
     PersonMap simpleStore = new PersonMap(); 
     // XmlConfigBuilder configBuilder = new XmlConfigBuilder(); 
     // Config config = configBuilder.build(); 
     MapConfig mapConfig = config.getMapConfig("personMap"); 

     MapStoreConfig mapStoreConfig = new MapStoreConfig(); 
     mapStoreConfig.setImplementation(simpleStore); 
     mapStoreConfig.setWriteDelaySeconds(0); 
     mapStoreConfig.setInitialLoadMode(InitialLoadMode.EAGER); 
     mapConfig.setMapStoreConfig(mapStoreConfig); 
     HazelcastInstance hz = Hazelcast.newHazelcastInstance(config); 
     IMap<Long, Person> personMap = hz.getMap("personMap"); 
     System.out.println(personMap); 
     Person p = personMap.get(1); 
     System.out.println(p); 

    } 
} 

请帮我加载data..its返回null 我tryed到两个nodes..but数据库中的数据不来运行它.. 可以数据库插入在地图synchronizely

回答

1

反映从快速浏览一下,你的HZ配置似乎很好。

几个问题:

allKeysStatement = con.prepareStatement( “SELECT * FROM人”);

您正在载入整个人,而不是钥匙。 Afaik的查询应该是'select person from person'

还要在PersonMap中添加一些日志语句(请将其重命名为PersonMapStore以防止名称混淆,您的PersonMap不是地图)。这样你可以看到哪些呼叫正在完成。特别是对loadAllKeys和loadAll的调用对于获取日志很重要。一旦你添加了日志记录功能,你可以更新你的文章,这样我们就可以看到发生了什么。