lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2007年11月24日

Jgroups配置与使用的学习笔记

Filed under: Java,JbossCache — 标签:, — lizongbo @ 14:56

Jgroups配置与使用的学习笔记
1.相关资料:
关于jgroups有几篇很好的中文介绍文章:
Rene Xu 的《Multicast and JGroups(未完待续)》
http://renexu.blogspot.com/2006/08/multicast-and-jgroups_28.html
还有Tim 的《JGroups 简介、适用场合、配置、程序例子Demo等完全使用指南》
http://hi.baidu.com/jabber/blog/item/7e879852a23efd0f0cf3e3ea.html
和Puras 的《JGroups之皮毛》
http://www.javaeye.com/post/292587

IP组播技术概述的资料:
http://net.pku.edu.cn/webCourse/ppt/lec14.pdf

jgroups的向导:
http://www.jgroups.org/javagroupsnew/docs/tutorial/html_single/index.html
jgroups的用户手册:
http://www.jgroups.org/javagroupsnew/docs/manual/html_single/index.html

2.jgroups 2.5及以上版本要求的jdk最低版本至少为JDK5.0,最小依赖的库文件为:commons-logging.jar,
jgroups-all.jar是一个包含了demo和junit单元测试用例的jar,如果想得到精简了的jar,
则可以下载源代码下来,使用 jar 任务编译得到jgroups-core.jar

操作步骤如下,解压JGroups-2.5.0.src.zip,得到D:\Java\JGroups-2.5.0.src。
然后命令行下运行build jar。

[code]

D:\Java\JGroups-2.5.0.src\lib>cd ..

D:\Java\JGroups-2.5.0.src>build jar
Buildfile: build.xml

prepare:
[mkdir] Created dir: D:\Java\JGroups-2.5.0.src\classes
[mkdir] Created dir: D:\Java\JGroups-2.5.0.src\dist
[mkdir] Created dir: D:\Java\JGroups-2.5.0.src\dist\javadoc

compile:
[javac] Compiling 508 source files to D:\Java\JGroups-2.5.0.src\classes
[javac] 注意:某些输入文件使用或覆盖了已过时的 API。
[javac] 注意:要了解详细信息,请使用 -Xlint:deprecation 重新编译。
[javac] 注意:某些输入文件使用了未经检查或不安全的操作。
[javac] 注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。

jgroups-all.jar:
[jar] Building jar: D:\Java\JGroups-2.5.0.src\dist\jgroups-all.jar

jgroups-core.jar:
[jar] Building jar: D:\Java\JGroups-2.5.0.src\dist\jgroups-core.jar

jar:

BUILD SUCCESSFUL
Total time: 2 minutes 37 seconds

[/code]
jgroups-core.jar比jgroups-all.jar小接近一半,在生产环境中,使用jgroups-core.jar即可。

3.查看jgroups版本信息: java -jar jgroups-all.jar

[code]

D:\Java\JGroups-2.5.0.src>cd dist

D:\Java\JGroups-2.5.0.src\dist>java -jar jgroups-all.jar

Version:        2.5.0
CVS:            $Id: Version.java,v 1.54 2007/07/02 14:39:09 belaban Exp $
History:        (see doc/history.txt for details)

[/code]

4.启动draw的demo。
运行 D:\Java\JGroups-2.5.0.src\bin\draw.bat,却提示出错。
[code]
2007-11-12 10:18:54 org.jgroups.stack.Protocol setPropertiesInternal
警告: down_thread was deprecated and is ignored
2007-11-12 10:18:54 org.jgroups.stack.Protocol setPropertiesInternal
警告: up_thread was deprecated and is ignored
2007-11-12 10:18:54 org.jgroups.stack.Protocol setPropertiesInternal
警告: down_thread was deprecated and is ignored
2007-11-12 10:18:54 org.jgroups.stack.Protocol setPropertiesInternal
警告: up_thread was deprecated and is ignored

——————————————————-
GMS: address is 10.108.20.126:2624
——————————————————-
2007-11-12 10:18:55 org.jgroups.stack.RouterStub connect
警告: RouterStub[local_address=10.108.20.126:2624,router_host=localhost,router_p
ort=5556,connected=false] failed connecting to localhost:5556
2007-11-12 10:18:55 org.jgroups.protocols.TUNNEL handleDownEvent
严重: failed connecting to GossipRouter at localhost:5556
2007-11-12 10:18:56 org.jgroups.stack.GossipClient _register
严重: exception connecting to host 127.0.0.1:5556
2007-11-12 10:18:58 org.jgroups.stack.GossipClient _getMembers
严重: exception connecting to host 127.0.0.1:5556
2007-11-12 10:18:58 org.jgroups.protocols.TCPGOSSIP sendGetMembersRequest
严重: [FIND_INITIAL_MBRS]: gossip client found no members
** View=[10.108.20.126:2624|0] [10.108.20.126:2624]
2007-11-12 10:19:00 org.jgroups.stack.GossipClient _getMembers
严重: exception connecting to host 127.0.0.1:5556
2007-11-12 10:19:00 org.jgroups.protocols.TCPGOSSIP sendGetMembersRequest
严重: [FIND_INITIAL_MBRS]: gossip client found no members
2007-11-12 10:19:05 org.jgroups.stack.GossipClient _getMembers
严重: exception connecting to host 127.0.0.1:5556
2007-11-12 10:19:05 org.jgroups.protocols.TCPGOSSIP sendGetMembersRequest
严重: [FIND_INITIAL_MBRS]: gossip client found no members
2007-11-12 10:19:07 org.jgroups.stack.GossipClient _getMembers

[/code]

4.启动drawnio的demo。

需要编辑D:\Java\JGroups-2.5.0.src\bin\drawnio.bat

将第10行LOCALHOSTIP的值改为本机真实 ip,LOCALHOSTIP=192.168.18.1

将最后的 “-props c:\jboss\JGroups\conf\tcp-nio.xml”改为“-props ..\conf\tcp-nio.xml”

修改后的D:\Java\JGroups-2.5.0.src\bin\drawnio.bat为:

[code]

@rem Convenience launcher for the Draw demo (contributed by Laran Evans lc278@cornell.edu)
@echo off

set CPATH=../classes;../conf;../lib/commons-logging.jar;../lib/log4j.jar;../lib/log4j-1.2.6.jar;../lib/concurrent.jar;../conf/log4j.properties

set JAVA_OPTS=
if -debug==%1 set JAVA_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_shmem,server=y,suspend=y,address=jgc1

if “%LOCALHOSTIP%”==”” echo Warning: You should set environment variable ‘LOCALHOSTIP’ to your local ip address before running this script.
if “%LOCALHOSTIP%”==”” set LOCALHOSTIP=10.108.20.126

@echo on
java -classpath %CPATH% %JAVA_OPTS% -Djgroups.bind_addr=%LOCALHOSTIP% -Djgroups.tcpping.initial_hosts=%LOCALHOSTIP%[7800],%LOCALHOSTIP%[7801] org.jgroups.demos.Draw -props ..\conf\tcp-nio.xml
[/code]

运行drawnio.bat,重复执行两次(或者三次),可以看到两个窗口(或者三个),在任意一个窗口上画图或者进行“clear”操作,图像信息均会被同步到其它窗口 。

5.测试两组不同的draw demo。

1.保持drawnio.bat与tcp-nio.xml的内容不变,将drawnio.bat运行两次。

2.修改drawnio.bat,-Djgroups.tcpping.initial_hosts=%LOCALHOSTIP%[7800],%LOCALHOSTIP%[7800]修改为 -Djgroups.tcpping.initial_hosts=%LOCALHOSTIP%[7803],%LOCALHOSTIP%[7804]。
修改tcp-nio.xml,start_port=”7803″改为 start_port=”7803″
(也可在bat里加上参数 -groupname aa)
将drawnio.bat运行两次。

在这启动的四个窗口中,两两分别为一组,信息同步。

6.分别在不同的机器上测试 draw demo.
修改drawnio.bat,-Djgroups.tcpping.initial_hosts=%LOCALHOSTIP%[7800],%LOCALHOSTIP%[7800]修改为 -Djgroups.tcpping.initial_hosts=10.108.20.126[7803],10.108.20.33[7803].

2007年11月5日

JBossCache 2.0.0的使用及注意事项

Filed under: cache,Java,JbossCache — 标签:, — lizongbo @ 15:42

JBossCache 2.0.0的使用及注意事项

1.首先是下载 JBossCache 2.0.0 GA.
下载地址为 :http://jaist.dl.sourceforge.net/sourceforge/jboss/JBossCache-all-2.0.0.GA.zip

来源 : http://sourceforge.net/project/showfiles.php?group_id=22866&package_id=102339

(目前最新版为: JBossCache 2.1.0.CR1 http://jaist.dl.sourceforge.net/sourceforge/jboss/jbosscache-core-2.1.0.CR1-all.zip)

2.解压到 D:\Java\JBossCache-all-2.0.0.GA。
D:\Java\JBossCache-all-2.0.0.GA\lib下是JbossCache需要用到的lib。

D:\Java\JBossCache-all-2.0.0.GA\docs\JBossCache-UserGuide\en\pdf\JBossCache-UserGuide.pdf是用户手册。
3.创建 Cache可以通过配置文件和默认的Cache工厂 。

[code]
XmlConfigurationParser parser = new XmlConfigurationParser();
Configuration conf = parser.parseFile(“E://JbossCacheservice.xml”);
Cache cache = DefaultCacheFactory.getInstance().createCache(conf, true);
[/code]

不过我遇到过郁闷的问题:
[code]
XmlConfigurationParser parser = new XmlConfigurationParser();
Configuration conf = parser.parseFile(“E://JbossCacheservice.xml”);
DefaultCacheFactory dcf=DefaultCacheFactory.getInstance();
Cache cache = dcf.createCache(conf, true);
[/code]
用DefaultCacheFactory.getInstance()来获取CacheFactory,却编译不了,会被提示:
“TestJbossCache.java”: incompatible types; no instance(s) of type variable(s) K,V exist so that org.jboss.cache.CacheFactory<K,V> conforms to org.jboss.cache.DefaultCacheFactory; found : <K,V>org.jboss.cache.CacheFactory<K,V>, required: org.jboss.cache.DefaultCacheFactory at line 13, column 49
4.配置文件

由于jbossCache 2.0 与 1.4 版本相比 变化较大,很多属性配置都已经被废弃或使用新的属性配置来代替了 。

CacheLoaderConfiguration被CacheLoaderConfig替代了。

集群的配置参数也有所变化 。

2007-11-05 10:59:24,671 WARN [org.jboss.cache.factories.XmlConfigurationParser]
– <Found non-existent JBC 1.x attribute ‘CacheLoaderConfiguration’ and replaced with ‘CacheLoaderConfig’. Please update your config to use the new attribute name>

缓存启动的时候不会把缓存在文件的内容加载到内存,如果此时遍历缓存节点,cache.getRoot()将得不到节点信息。
5.java测试代码:

[code]
package com.lizongbo.jbosscache;

import org.jboss.cache.factories.XmlConfigurationParser;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.DefaultCacheFactory;
import org.jboss.cache.Fqn;
import org.jboss.cache.Cache;

import java.util.*;

public class TestJbossCacheMaster {
public static void main(String[] args) throws InterruptedException {
XmlConfigurationParser parser = new XmlConfigurationParser();
Configuration conf = parser.parseFile(“E://JbossCacheservice_master.xml”);
Cache cache = DefaultCacheFactory.getInstance().createCache(conf, true);
Fqn fqn = Fqn.fromString(“/domains/nsinfo”);
JbossCacheTestListener listener = new JbossCacheTestListener();
cache.addCacheListener(listener);
System.out.println(” root is:” + cache.getRoot());
System.out.println(“缓存的 name server为: ” + cache.get(fqn, “618119.com”));
System.out.println(” after cache.get root is:” + cache.getRoot());
List<String> l = new ArrayList<String> ();
l.add(“ns1.dnspod.net”);
l.add(“ns2.dnspod.net”);
l.add(“ns3.dnspod.net”);
l.add(“ns4.dnspod.net”);
l.add(“ns5.dnspod.net”);
l.add(“ns6.dnspod.net”);
cache.put(fqn, “618119.com”, l);
System.out.println(“缓存的 name server为: ” + cache.get(fqn, “618119.com”));
Thread.sleep(500000);
}
}
[/code]

xml配置文件:

[code]

<?xml version=”1.0″ encoding=”UTF-8″?>
<server>
<mbean code=”org.jboss.cache.jmx.CacheJmxWrapper” name=”jboss.cache:service=Cache”>
<depends>jboss:service=Naming</depends>
<depends>jboss:service=TransactionManager</depends>
<attribute name=”TransactionManagerLookupClass”>org.jboss.cache.transaction.GenericTransactionManagerLookup</attribute>
<attribute name=”IsolationLevel”>REPEATABLE_READ</attribute>
<attribute name=”CacheMode”>REPL_ASYNC</attribute>
<attribute name=”UseReplQueue”>false</attribute>
<attribute name=”ReplQueueInterval”>0</attribute>
<attribute name=”ReplQueueMaxElements”>0</attribute>
<attribute name=”ClusterName”>JBossCache-Cluster</attribute>
<depends optional-attribute-name=”MultiplexerService”
proxy-type=”attribute”>jgroups.mux:name=Multiplexer</depends>
<attribute name=”MultiplexerStack”>udp</attribute>
<attribute name=”ClusterConfig”>
<config>
<UDP mcast_addr=”228.1.2.3″ mcast_port=”48866″
ip_ttl=”64″ ip_mcast=”true”
mcast_send_buf_size=”150000″ mcast_recv_buf_size=”80000″
ucast_send_buf_size=”150000″ ucast_recv_buf_size=”80000″
loopback=”false”/>
<PING timeout=”2000″ num_initial_members=”3″
up_thread=”false” down_thread=”false”/>
<MERGE2 min_interval=”10000″ max_interval=”20000″/>
<FD_SOCK/>
<VERIFY_SUSPECT timeout=”1500″
up_thread=”false” down_thread=”false”/>
<pbcast.NAKACK gc_lag=”50″ retransmit_timeout=”600,1200,2400,4800″
max_xmit_size=”8192″ up_thread=”false” down_thread=”false”/>
<UNICAST timeout=”600,1200,2400″ down_thread=”false”/>
<pbcast.STABLE desired_avg_gossip=”20000″
up_thread=”false” down_thread=”false”/>
<pbcast.GMS join_timeout=”5000″ join_retry_timeout=”2000″
shun=”true” print_local_addr=”true”/>
<FC max_credits=”2000000″ down_thread=”false” up_thread=”false”
min_threshold=”0.20″/>
<FRAG frag_size=”8192″ down_thread=”false” up_thread=”true”/>
<pbcast.STATE_TRANSFER up_thread=”true” down_thread=”true”/>
</config>
</attribute>
<attribute name=”InitialStateRetrievalTimeout”>20000</attribute>
<attribute name=”SyncReplTimeout”>20000</attribute>
<attribute name=”LockAcquisitionTimeout”>15000</attribute>
<attribute name=”FetchInMemoryState”>false</attribute>
<attribute name=”CacheLoaderConfig”>
<config>
<passivation>false</passivation>
<shared>false</shared>
<cacheloader>
<class>org.jboss.cache.loader.FileCacheLoader</class>
<properties>
location=/tmp/filecacheloader_master
</properties>
<async>false</async>
<fetchPersistentState>false</fetchPersistentState>
<ignoreModifications>false</ignoreModifications>
<purgeOnStartup>false</purgeOnStartup>
</cacheloader>
</config>
</attribute>
</mbean>
</server>

[/code]

2007年11月2日

jbossCache在Tomcat中部署时出现javax.naming.NamingException: Context is read only

Filed under: cache,Java,JbossCache — 标签:, , , , , — lizongbo @ 11:41

出错信息为:

[code]
2007-11-02 11:33:41,471 ERROR [org.jboss.cache.transaction.DummyTransactionManager] – <binding of DummyTransactionManager failed>
javax.naming.NamingException: Context is read only
at org.apache.naming.NamingContext.checkWritable(NamingContext.java:903)

at org.apache.naming.NamingContext.bind(NamingContext.java:831)
at org.apache.naming.NamingContext.bind(NamingContext.java:171)
at org.apache.naming.NamingContext.bind(NamingContext.java:187)
at org.apache.naming.SelectorContext.bind(SelectorContext.java:171)
at javax.naming.InitialContext.bind(InitialContext.java:359)
at org.jboss.cache.transaction.DummyTransactionManager.getInstance(DummyTransactionManager.java:42)
at org.jboss.cache.transaction.GenericTransactionManagerLookup

.getTransactionManager (GenericTransactionManagerLookup.java:92)
at org.jboss.cache.CacheImpl.createTransactionManager(CacheImpl.java:660)
at org.jboss.cache.CacheImpl.internalStart(CacheImpl.java:726)
at org.jboss.cache.CacheImpl.start(CacheImpl.java:708)
at org.jboss.cache.DefaultCacheFactory.createCache(DefaultCacheFactory.java:79)
[/code]
这是因为我们通常是使用了JbossCache 自带的配置文件样本来进行配置的。
配置文件中默认是这样配置的:
<attribute name=”TransactionManagerLookupClass”>org.jboss.cache.transaction.GenericTransactionManagerLookup
</attribute>

而通过查看 org.jboss.cache.transaction.DummyTransactionManager.java的代码可以发现,
在创建TransactionManager的时候执行了一个Context。bind绑定操作。
出错信息用 log.error写入日志里:

代码为:[code]
public static DummyTransactionManager getInstance()
{
if (instance == null)
{
instance = new DummyTransactionManager();
try
{
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, “org.jboss.cache.transaction.DummyContextFactory”);
Context ctx = new InitialContext(p);
ctx.bind(“java:/TransactionManager”, instance);
ctx.bind(“UserTransaction”, new DummyUserTransaction(instance));
}
catch (NamingException e)
{
log.error(“binding of DummyTransactionManager failed”, e);
}
}
return instance;
}
[/code]

jboss官方网站有相关讨论:
http://jboss.org/?module=bb&op=viewtopic&t=98997

解决办法也很简单,就是将 TransactionManagerLookupClass指定为自己实现的MyTransactionManagerLookup

MyTransactionManagerLookup。java 代码如下 :
[code]

package com.lizongbo.jbosscache;

import org.jboss.cache.transaction.TransactionManagerLookup;
import javax.transaction.TransactionManager;
import org.jboss.cache.transaction.BatchModeTransactionManager;

public class MyTransactionManagerLookup implements TransactionManagerLookup {
public TransactionManager getTransactionManager() throws Exception {
return new BatchModeTransactionManager();
}

}
[/code]

修改后的配置文件为:

<!–
Configure the TransactionManager
–>
<attribute name=”TransactionManagerLookupClass”>com.lizongbo.jbosscache.MyTransactionManagerLookup</attribute>

Powered by WordPress