接上面java actor模型框架ujavaactor http://zhwj184.iteye.com/admin/blogs/1613351,上面的示例比较复杂,写一个简单点的示例:
import java.util.HashMap;
import java.util.Map;
import com.ibm.actor.AbstractActor;
import com.ibm.actor.Actor;
import com.ibm.actor.DefaultActorManager;
import com.ibm.actor.DefaultMessage;
import com.ibm.actor.Message;
public class TestActor extends AbstractActor {
private Actor to;
public void setTestActor(Actor sendTo) {
this.to = sendTo;
}
@Override
public void activate() {
super.activate();
}
@Override
public void deactivate() {
logger.trace("TestActor deactivate: %s", getName());
super.deactivate();
}
@Override
protected void runBody() {
DefaultMessage m = new DefaultMessage("message->from :" + getName(), 20);
getManager().send(m, null, this);
}
@Override
protected void loopBody(Message m) {
String subject = m.getSubject();
int count = (Integer) m.getData();
if (count > 0) {
m = new DefaultMessage("message->from :" + getName(), count - 1);
System.out.println(getName() + ":" + subject + " data:" + m.getData());
getManager().send(m, this, to);
}
}
public static void main(String[] args) {
DefaultActorManager am = new DefaultActorManager();
Map<String, Actor> actors = new HashMap<String, Actor>();
try {
Map<String, Object> options = new HashMap<String, Object>();
options.put(DefaultActorManager.ACTOR_THREAD_COUNT, 2);
am.initialize(options);
TestActor a = (TestActor) am.createActor(TestActor.class, "actor0");
actors.put(a.getName(), a);
TestActor b = (TestActor) am.createActor(TestActor.class, "actor1");
actors.put(b.getName(), b);
a.setTestActor(b);
b.setTestActor(a);
for (String key : actors.keySet()) {
am.startActor(actors.get(key));
}
Thread.sleep(10000000);
// am.terminateAndWait();
} catch (Exception e) {
}
}
}
输出:
actor0:message->from :actor0 data:19
actor1:message->from :actor1 data:19
actor1:message->from :actor0 data:18
actor0:message->from :actor1 data:18
actor1:message->from :actor0 data:17
actor0:message->from :actor1 data:17
actor0:message->from :actor1 data:16
actor1:message->from :actor0 data:16
actor1:message->from :actor0 data:15
actor0:message->from :actor1 data:15
actor0:message->from :actor1 data:14
actor1:message->from :actor0 data:14
actor0:message->from :actor1 data:13
actor1:message->from :actor0 data:13
actor0:message->from :actor1 data:12
actor1:message->from :actor0 data:12
actor1:message->from :actor0 data:11
actor0:message->from :actor1 data:11
actor0:message->from :actor1 data:10
actor1:message->from :actor0 data:10
actor0:message->from :actor1 data:9
actor1:message->from :actor0 data:9
actor1:message->from :actor0 data:8
actor0:message->from :actor1 data:8
actor0:message->from :actor1 data:7
actor1:message->from :actor0 data:7
actor1:message->from :actor0 data:6
actor0:message->from :actor1 data:6
actor0:message->from :actor1 data:5
actor1:message->from :actor0 data:5
actor1:message->from :actor0 data:4
actor0:message->from :actor1 data:4
actor1:message->from :actor0 data:3
actor0:message->from :actor1 data:3
actor1:message->from :actor0 data:2
actor0:message->from :actor1 data:2
actor1:message->from :actor0 data:1
actor0:message->from :actor1 data:1
actor1:message->from :actor0 data:0
actor0:message->from :actor1 data:0