当前位置:web 服务器 > Tomcat >>

Tomcat的Cluster功能复制session时会复制保存在其中的对象么?

答案:
问题:当业务使用了SSESSION保存某些对象,怎样在集群时复制这些对象?

TOMCAT5已经考虑过这种集群所导致的问题,其实也就是cluster的问题。如果采用了TOMCAT 的cluster解决方案,我相信应该可以保证session等信息的同步可操作。

另外,TOMCAT要求放到SESSION里的对象要需要实现Serializable,以确保各个对象能被正常复制。

有人提出疑问,SESSION使用的是MAP来保存数据,序列化时不会把实际的对象序列化。其实看看HashMap序列化的代码就可以知道,在SESSION序列化时,是可以正确将Map中保存的数据对象序列化的。更主要的是,TOMCAT序列化SESSION时,并没有使用HashMap的序列化方式,而是将各个保存的对象读出来单独进行序列化。

因此保存到SESSION中的对象实例应该也会被复制。

这样就可以保证各个服务器上的内存数据一样了。

另外一个问题是许多人担心 TOMCAT 的这个解决发方案的性能问题,其实同步某某些内容信息如SESSION的代价并不是很大的,特别在几台工作站属于同一个网内。想对其带来的横向扩展性能,可以忽略,我想。

参考:

TOMCAT中SESSION序列化的代码:

protected void writeObject(ObjectOutputStream stream) throws IOException {

        // Write the scalar instance variables (except Manager)        stream.writeObject(new Long(creationTime));        stream.writeObject(new Long(lastAccessedTime));        stream.writeObject(new Integer(maxInactiveInterval));        stream.writeObject(new Boolean(isNew));        stream.writeObject(new Boolean(isValid));        stream.writeObject(new Long(thisAccessedTime));        stream.writeObject(id);        if (debug >= 2)            log("writeObject() storing session " + id);

        // Accumulate the names of serializable and non-serializable attributes        String keys[] = keys();        ArrayList saveNames = new ArrayList();        ArrayList saveValues = new ArrayList();        for (int i = 0; i < keys.length; i++) {            Object value = null;            synchronized (attributes) {                value = attributes.get(keys[i]);            }            if (value == null)                continue;            else if (value instanceof Serializable) {                saveNames.add(keys[i]);                saveValues.add(value);            }        }

        // Serialize the attribute count and the Serializable attributes        int n = saveNames.size();        stream.writeObject(new Integer(n));        for (int i = 0; i < n; i++) {            stream.writeObject((String) saveNames.get(i));            try {                stream.writeObject(saveValues.get(i));                if (debug >= 2)                    log("  storing attribute '" + saveNames.get(i) +                        "' with value '" + saveValues.get(i) + "'");            } catch (NotSerializableException e) {                log(sm.getString("standardSession.notSerializable",                                 saveNames.get(i), id), e);                stream.writeObject(NOT_SERIALIZED);                if (debug >= 2)                    log("  storing attribute '" + saveNames.get(i) +                        "' with value NOT_SERIALIZED");            }        }

    }

HashMap序列化的代码:

private void writeObject(java.io.ObjectOutputStream s)        throws IOException    { // Write out the threshold, loadfactor, and any hidden stuff s.defaultWriteObject();

 // Write out number of buckets s.writeInt(table.length);

 // Write out size (number of Mappings) s.writeInt(size);

        // Write out keys and values (alternating)        for (Iterator i = entrySet().iterator(); i.hasNext(); ) {            Map.Entry e = (Map.Entry) i.next();            s.writeObject(e.getKey());            s.writeObject(e.getValue());        }    }

上一个:看看多种Look And Feel开发包所展示的效果如何
下一个:虚拟机概论(九)——虚拟机总结

Apache
IIS
Nginx
Tomcat
如果你遇到web 服务器难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,