<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体;} pre {margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Courier New"; mso-fareast-font-family:宋体;} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:1169323925; mso-list-type:hybrid; mso-list-template-ids:-634083670 673468032 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-text:%1、; mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in;} ol {margin-bottom:0in;} ul {margin-bottom:0in;} -->
JBOSS cache AS5和 J2SE程序之间通信的一个例子
想在 JBOSS AS5中利用 JBOSS Cache和 J2SE程序之间进行通信, google了一把,没有发现这样的例子。经过摸索后在 JBOSS AS5和 J2SE程序之间利用 JBOSS cache成功地进行了通信。
1、 JBOSS AS5中利用 JBOSS Cache
在 JBOSS AS5的手册中,讲解了如何通过 CacheManager在两个 JBOSS AS5之间通信,代码大致如下:
… import javax.naming.InitialContext; import org.jboss.cache.Cache; import org.jboss.cache.Node; import org.jboss.cache.Fqn; import org.jboss.ha.cachemanager.CacheManager;
… InitialContext ctx = new InitialContext(); CacheManager cacheManager = (CacheManager )ctx.lookup( "java:CacheManager"); cacheManager.start();
Cache cache = cacheManager.getCache("my-test-cache", true);
if(cache.getCacheStatus().allowInvocations() ) { cache.start(); }
Fqn childFqn1 = Fqn.fromString("/child1"); Node node = cache.getRoot().addChild( childFqn1 ); … node.put( “test_key”, “test_value_123); …
|
上面的代码片段放入 J2EE程序中,在 $JBOSS_HOME/server/all/deploy/cluster/jboss-cache-manager.sar/jboss-cache-manager-jboss-beans.xml文件中添加一项 :
<entry><key>my-test-cache</key> <value>
<bean name="MyTestCache" class="org.jboss.cache.config.Configuration">
<!-- Provides batching functionality for caches that don't want to interact with regular JTA Transactions --> <property name="transactionManagerLookupClass">org.jboss.cache.transaction.BatchModeTransactionManagerLookup</property>
<property name="clusterName">${jboss.partition.name:DefaultPartition}-MyTestCache</property> <property name="multiplexerStack">${jboss.default.jgroups.stack:udp}</property> <property name="fetchInMemoryState">true</property>
<property name="nodeLockingScheme">PESSIMISTIC</property> <property name="isolationLevel">REPEATABLE_READ</property> <property name="useLockStriping">false</property> <property name="cacheModeString">REPL_SYNC</property>
<property name="syncReplTimeout">17500</property> <property name="lockAcquisitionTimeout">15000</property> <property name="stateRetrievalTimeout">60000</property>
<property name="useRegionBasedMarshalling">false</property> <property name="inactiveOnStartup">false</property>
<!-- Disable asynchronous RPC marshalling/sending --> <property name="serializationExecutorPoolSize">0</property> <!-- We have no asynchronous notification listeners --> <property name="listenerAsyncPoolSize">0</property>
<property name="exposeManagementStatistics">true</property> </bean> </value> </entry> |
编辑完后存盘。
运行 jboss AS5:
$cd $JBOSS_HOME
$bin/run.sh –c all
发布包含上面代码的 J2EE程序。
2、 在 J2SE中利用 JBOSS Cache
J2SE中使用 JBOSS Cache的代码大概如下:
… import org.jboss.cache.Cache; import org.jboss.cache.Fqn; import org.jboss.cache.Node; import org.jboss.ha.cachemanager.CacheManager; import org.jboss.ha.framework.server.JChannelFactory;
… String channelConfigName = "jgroups-channelfactory-stacks.xml"; JChannelFactory channelFactory = new JChannelFactory(); channelFactory.setMultiplexerConfig( channelConfigName, true ); channelFactory.start();
CacheManager cacheMgr = new CacheManager( "cache-configs.xml", channelFactory ); cacheMgr.start(); cache = cacheMgr.getCache( "my-test-cache", true ); cache.start();
Fqn childFqn1 = Fqn.fromString("/child1"); Node node = cache.getRoot().addChild( childFqn1 ); … String value =(String)node.get( “test_key”); …
|
将 jgroups-channelfactory-stacks.xml从 JBOSS AS5中拷贝到 J2SE中:
$cp $JBOSS_HOME/server/all/deploy/cluster/jgroups-channelfactory.sar/META-INF/ jgroups-channelfactory-stacks.xml .
将在 JBOSS AS5中配置的 ”my-test-cache”从 $JBOSS_HOME/server/all/deploy/cluster/jboss-cache-manager.sar/jboss-cache-manager-jboss-beans.xml文件中拷贝出来,进行格式变换,变成 jboss cache要求的格式。变换后, ”my-test-cache”的格式如下:
<cache-configs> … <cache-config name="my-test-cache">
<!-- Provides batching functionality for caches that don't want to interact with regular JTA Transactions --> <attribute name="TransactionManagerLookupClass">org.jboss.cache.transaction.BatchModeTransactionManagerLookup</attribute>
<attribute name="clusterName">${jboss.partition.name:DefaultPartition}-MyTestCache</attribute> <attribute name="multiplexerStack">${jboss.default.jgroups.stack:udp}</attribute> <attribute name="FetchInMemoryState">true</attribute>
<attribute name="NodeLockingScheme">PESSIMISTIC</attribute> <attribute name="IsolationLevel">REPEATABLE_READ</attribute> <attribute name="UseLockStriping">false</attribute> <attribute name="CacheModeString">REPL_SYNC</attribute>
<attribute name="SyncReplTimeout">17500</attribute> <attribute name="LockAcquisitionTimeout">15000</attribute> <attribute name="StateRetrievalTimeout">60000</attribute>
<attribute name="UseRegionBasedMarshalling">false</attribute> <attribute name="InactiveOnStartup">false</attribute>
<!-- Disable asynchronous RPC marshalling/sending --> <attribute name="SerializationExecutorPoolSize">0</attribute> <!-- We have no asynchronous notification listeners --> <attribute name="ListenerAsyncPoolSize">0</attribute>
<attribute name="ExposeManagementStatistics">true</attribute> </cache-config> … </cache-configs> |
将此文件存为 cache-configs.xml。
将如下的 .jar文件从 JBOSS AS5中拷贝到 J2SE中:
commons-logging.jar
jboss-bootstrap.jar
jbosscache-core.jar
jbosscache-pojo.jar
jboss-common-core.jar
jbossha.jar
jboss-ha-server-api.jar
jboss-ha-server-cache-jbc.jar
jboss-ha-server-cache-spi.jar
jbossjta.jar
jbossjts.jar
jboss-logging-spi.jar
jboss-transaction-api.jar
jcip-annotations.jar
jgroups.jar
log4j.jar
然后对 J2SE程序进行编译并运行即可。这样 JBOSS AS5和 J2SE程序就可以通信了。
JBOSS AS5和 J2SE程序利用 JBOSS Cache通信的关键是利用 org.jboss.ha.cachemanager.CacheManager和 org.jboss.ha.framework.server.JchannelFactory而不是利用 JBOSS Cache中的 org.jboss.cache.DefaultCacheFactory
管理 Cache的创建工作。