2. Terracotta Eclipse Plugin
开发基于Terracotta的应用程序的最便捷的方法就是使用TerracottaEclipse Plugin。http://www.terracotta.org/web/display/docs/Eclipse+Plugin+Guide上有详细的安装说明。安装完毕后,通过Terracotta->Add Terracotta Nature可以给已有的工程增加Terracotta Nature。此外也可以通过File->New->Project->Java->Terracotta Projects->Terracotta DSO Project创建一个Terracotta工程。Terracotta工程创建完毕后,工程的根目录下会创建tc-config.xml、terracotta目录和一个Boot jar。tc-config.xml包含了所有Terracotta相关的配置信息,例如需要进行字节码加强的类、Lock相关的配置,共享对象的root、分布式方法的配置等。Terracotta目录用于保存Terracotta客户端的日志和统计信息等。Boot jar的作用会在稍后的章节里介绍。接下来通过两个例子介绍一下Terracotta Eclipse Plugin的使用。
2.1 wait/notify
设想某个线程A调用了某个对象obj的wait方法后被阻塞,接下来另一个线程B调用了obj的notify方法从而唤醒了线程A。这在单个JVM中是司空见惯的场景了。但是有没有设想过B线程可以在一个不同于线程A所在的JVM中调用obj的notify方法从而唤醒线程A呢?这在Terracotta的世界里也是司空见惯的场景。
首先建立一个Terracotta工程,然后创建以下两个普通的Java类:
package tcinaction; public class A { public static final Object OBJECT = new Object(); public static void main(String args[]) throws InterruptedException { System.out.println("A is waiting on OBJECT..."); synchronized(OBJECT) { OBJECT.wait(); } System.out.println("A was woken up"); } } package tcinaction; public class B { public static void main(String args[]) throws InterruptedException { System.out.println("B is calling A.OBJECT.notify()..."); synchronized(A.OBJECT) { A.OBJECT.notify(); } } }
然后在Package Explorer中选中A,单击右键选中Terracotta->Module A.java->Instrumented,也就是令Terracotta对A类进行字节码加强。对B类也执行同样操作。接下来在A类的OBJECT静态成员变量上单击右键,选中Terracotta->Field OBJECT->Shared root,这样A类的OBJECT就成了在Terracotta中共享的对象。再接下来在A类的main方法上单击右键,选中Terracotta->Method main->Autolock,在弹出的Specify Autolock Attributes对话框中选中Write。对B类的main方法也执行相同的操作。经过了以上操作之后,tc-config.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?> <con:tc-config xmlns:con="http://www.terracotta.org/config"> <servers> <server host="%i" name="localhost"> <dso-port>9510</dso-port> <jmx-port>9520</jmx-port> <data>terracotta/server-data</data> <logs>terracotta/server-logs</logs> <statistics>terracotta/cluster-statistics</statistics> </server> </servers> <clients> <logs>terracotta/client-logs</logs> <statistics>terracotta/client-statistics/%D</statistics> </clients> <application> <dso> <instrumented-classes> <include> <class-expression>tcinaction.A</class-expression> </include> <include> <class-expression>tcinaction.B</class-expression> </include> </instrumented-classes> <roots> <root> <field-name>tcinaction.A.OBJECT</field-name> </root> </roots> <locks> <autolock auto-synchronized="false"> <method-expression>void tcinaction.B.main(java.lang.String[])</method-expression> <lock-level>write</lock-level> </autolock> <autolock auto-synchronized="false"> <method-expression>void tcinaction.A.main(java.lang.String[])</method-expression> <lock-level>write</lock-level> </autolock> </locks> </dso> </application> </con:tc-config>
最后我们来验证一下程序的运行结果。首先选中A类,单击右键Run As->Terracotta DSO Application。此时首先会启动Terracotta server,然后会启动另外一个JVM,运行A类的main方法。输出如下:
A is waiting on OBJECT...
接下来选中B类,单击右键Run As->Terracotta DSO Application。此时会再启动一个JVM,执行B类的main方法,相关控制台的输出如下:
B is calling A.OBJECT.notify()...
回到A的控制台,发现其主线程已被唤醒并执行完毕,输出如下:
A is waiting on OBJECT...
A was woken up
2.2 Simple Messaging
在这个例子中介绍一个通过LinkedBlockingQueue在不同JVM中传递数据的方法。首先创建以下两个类:
package tcinaction; import java.util.concurrent.LinkedBlockingQueue; public class MessageProducer { public static final LinkedBlockingQueue<String> PIPE = new LinkedBlockingQueue<String>(); public static void main(String args[]) throws InterruptedException { for(int i = 0; i < 100; i++) { PIPE.offer("message-" + i); Thread.sleep(1000); } } } package tcinaction; public class MessageConsumer { public static void main(String args[]) throws InterruptedException { while(true) { String message = MessageProducer.PIPE.take(); System.out.println(message); } } }
然后将MessageProducer 和 MessageConsumer配置为instrumented;各自的main方法配置为Autolock(Write);MessageProducer的PIPE静态成员变量配置为Shared root。此时tc-config.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?> <con:tc-config xmlns:con="http://www.terracotta.org/config"> <servers> <server host="%i" name="localhost"> <dso-port>9510</dso-port> <jmx-port>9520</jmx-port> <data>terracotta/server-data</data> <logs>terracotta/server-logs</logs> <statistics>terracotta/cluster-statistics</statistics> </server> </servers> <clients> <logs>terracotta/client-logs</logs> <statistics>terracotta/client-statistics/%D</statistics> </clients> <application> <dso> <instrumented-classes> <include> <class-expression>tcinaction.MessageProducer</class-expression> </include> <include> <class-expression>tcinaction.MessageConsumer</class-expression> </include> </instrumented-classes> <locks> <autolock auto-synchronized="false"> <method-expression>void tcinaction.MessageConsumer.main(java.lang.String[])</method-expression> <lock-level>write</lock-level> </autolock> <autolock auto-synchronized="false"> <method-expression>void tcinaction.MessageProducer.main(java.lang.String[])</method-expression> <lock-level>write</lock-level> </autolock> </locks> <roots> <root> <field-name>tcinaction.MessageProducer.PIPE</field-name> </root> </roots> </dso> </application> </con:tc-config>
最后我们来验证一下程序的运行结果。首先启动MessageConsumer,单击右键Run As->Terracotta DSO Application。然后启动MessageProducer。MessageConsumer的控制台上会显示出接收到的消息:
message-0
message-1
message-2
…