2012年9月23日星期日

Coherence_004:Coherence入门指南之四:访问复杂对象

运行环境:JDeveloper 11.1.2.2.0 + Coherence3.7.1 + Oracle Database 10g Express Edition 10.2.0.1。

上一个实验放到Cache中的是最普通的String类型的对象,在这个实验中将向Cache中放复杂一些的对象。

由于Coherence集群需要通过网络传输对象,并且涉及到读写操作,因此要求对象必须Serializable。
所谓序列化,就是把内存中对象的状态保存在硬盘上的一个二进制的byte序列中,而反序列化则是把该byte序列重新构建为内存中的对象。

1. Person.java

package com.oracle.coherence.handson;


import java.io.Serializable;

public class Person implements Serializable {
 
    private int id;
    private String surname;
    private String firstname;
    private String address;
    private int age;
    private String gender;
 
    public static String MALE = "M";
    public static String FEMALE = "F";
 
    public Person() {
    }

    public Person(int id1, String surname1, String firstname1, String address1,
                  int age1, String gender1) {
        super();
        this.id = id1;
        this.surname = surname1;
        this.firstname = firstname1;
        this.address = address1;
        this.age = age1;
        this.gender = gender1;
    }

    public void setId(int param) {
        this.id = param;
    }

    public int getId() {
        return id;
    }

    public void setSurname(String param) {
        this.surname = param;
    }

    public String getSurname() {
        return surname;
    }

    public void setFirstname(String param) {
        this.firstname = param;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setAddress(String param) {
        this.address = param;
    }

    public String getAddress() {
        return address;
    }

    public void setAge(int param) {
        this.age = param;
    }

    public int getAge() {
        return age;
    }

    public void setGender(String param) {
        this.gender = param;
    }

    public String getGender() {
        return gender;
    }

    public double getAgeDouble() {
        return (double)this.age;
    }

    @Override
    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (!(object instanceof Person)) {
            return false;
        }
        final Person other = (Person)object;
        if (id != other.id) {
            return false;
        }
        if (!(surname == null ? other.surname == null : surname.equals(other.surname))) {
            return false;
        }
        if (!(firstname == null ? other.firstname == null : firstname.equals(other.firstname))) {
            return false;
        }
        if (!(address == null ? other.address == null : address.equals(other.address))) {
            return false;
        }
        if (age != other.age) {
            return false;
        }
        if (!(gender == null ? other.gender == null : gender.equals(other.gender))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        final int PRIME = 37;
        int result = 1;
        result = PRIME * result + id;
        result = PRIME * result + ((surname == null) ? 0 : surname.hashCode());
        result = PRIME * result + ((firstname == null) ? 0 : firstname.hashCode());
        result = PRIME * result + ((address == null) ? 0 : address.hashCode());
        result = PRIME * result + age;
        result = PRIME * result + ((gender == null) ? 0 : gender.hashCode());
        return result;
    }
}

2. PersonCache.java

package com.oracle.coherence.handson;

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;

public class PersonCache {
    public PersonCache() {
    }

    public static void main(String[] args) {

        long startTime = System.currentTimeMillis();

        NamedCache person = CacheFactory.getCache("person");

        Person p1 = new Person(1, "Middleton", "Tim", "Level 2, 66 Kings Park Road, West Perth", 39, Person.MALE);

        person.put(p1.getId(), p1);

        Person p2 = (Person)person.get(p1.getId());

        if (p2.equals(p1)) {
            System.out.println("They are the same!!");
        }

        long endTime = System.currentTimeMillis();

        System.out.println("### Time spend: " + (endTime - startTime));
    }
}

3. PersonAdvanced.java
Coherence特别提供了一个序列化/反序列化的工具类,据说比Java默认的实现速度高6倍。
要使用该工具类,需要实现ExternalizableLite接口,并且重写方法readExternal和writeExternal。

package com.oracle.coherence.handson;


import com.tangosol.io.ExternalizableLite;
import com.tangosol.util.ExternalizableHelper;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class PersonAdvanced implements ExternalizableLite {

    private int id;
    private String surname;
    private String firstname;
    private String address;
    private int age;
    private String gender;

    public static String MALE = "M";
    public static String FEMALE = "F";

    public PersonAdvanced() {
    }

    public PersonAdvanced(int id1, String surname1, String firstname1, String address1, int age1, String gender1) {
        super();
        this.id = id1;
        this.surname = surname1;
        this.firstname = firstname1;
        this.address = address1;
        this.age = age1;
        this.gender = gender1;
    }

    public void setId(int param) {
        this.id = param;
    }

    public int getId() {
        return id;
    }

    public void setSurname(String param) {
        this.surname = param;
    }

    public String getSurname() {
        return surname;
    }

    public void setFirstname(String param) {
        this.firstname = param;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setAddress(String param) {
        this.address = param;
    }

    public String getAddress() {
        return address;
    }

    public void setAge(int param) {
        this.age = param;
    }

    public int getAge() {
        return age;
    }

    public void setGender(String param) {
        this.gender = param;
    }

    public String getGender() {
        return gender;
    }

    public double getAgeDouble() {
        return (double)this.age;
    }

    @Override
    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (!(object instanceof PersonAdvanced)) {
            return false;
        }
        final PersonAdvanced other = (PersonAdvanced)object;
        if (id != other.id) {
            return false;
        }
        if (!(surname == null ? other.surname == null : surname.equals(other.surname))) {
            return false;
        }
        if (!(firstname == null ? other.firstname == null : firstname.equals(other.firstname))) {
            return false;
        }
        if (!(address == null ? other.address == null : address.equals(other.address))) {
            return false;
        }
        if (age != other.age) {
            return false;
        }
        if (!(gender == null ? other.gender == null : gender.equals(other.gender))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        final int PRIME = 37;
        int result = 1;
        result = PRIME * result + ((surname == null) ? 0 : surname.hashCode());
        result = PRIME * result + ((firstname == null) ? 0 : firstname.hashCode());
        result = PRIME * result + ((address == null) ? 0 : address.hashCode());
        result = PRIME * result + ((gender == null) ? 0 : gender.hashCode());
        return result;
    }

    public void readExternal(DataInput dataInput) throws IOException {
        this.id = ExternalizableHelper.readInt(dataInput);
        this.surname = ExternalizableHelper.readSafeUTF(dataInput);
        this.firstname = ExternalizableHelper.readSafeUTF(dataInput);
        this.address = ExternalizableHelper.readSafeUTF(dataInput);
        this.gender = ExternalizableHelper.readSafeUTF(dataInput);
        this.age = ExternalizableHelper.readInt(dataInput);
    }

    public void writeExternal(DataOutput dataOutput) throws IOException {
        ExternalizableHelper.writeInt(dataOutput, this.id);
        ExternalizableHelper.writeSafeUTF(dataOutput, this.surname);
        ExternalizableHelper.writeSafeUTF(dataOutput, this.firstname);
        ExternalizableHelper.writeSafeUTF(dataOutput, this.address);
        ExternalizableHelper.writeSafeUTF(dataOutput, this.gender);
        ExternalizableHelper.writeInt(dataOutput, this.age);
    }
}

4. PersonAdvancedCache.java

package com.oracle.coherence.handson;

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;

public class PersonAdvancedCache {
    public PersonAdvancedCache() {
    }

    public static void main(String[] args) {

        long startTime = System.currentTimeMillis();

        NamedCache person = CacheFactory.getCache("person");

        PersonAdvanced p1 = new PersonAdvanced(1, "Middleton", "Tim", "Level 2, 66 Kings Park Road, West Perth", 39, PersonAdvanced.MALE);

        person.put(p1.getId(), p1);

        PersonAdvanced p2 = (PersonAdvanced)person.get(p1.getId());

        if (p2.equals(p1)) {
            System.out.println("They are the same!!");
        }
     
        long endTime = System.currentTimeMillis();

        System.out.println("### Time spend: " + (endTime - startTime));
    }
}

5. 运行
(1)选择运行DefaultCacheServer Profile,作为存储数据的Cache。
(2)选择NoLocalStorage Profile,并运行PersonCache。
(3)选择NoLocalStorage Profile,并运行PersonAdvancedCache。

Project 下载:CoherenceApp(Lab4).7z

没有评论: