java actor模型和消息传递简单示例

本文通过一个简单的 JavaActor 模型示例展示了如何使用 UJavaActor 框架进行消息传递与处理。两个 Actor 实例相互发送消息,并递减计数直至为零,演示了基本的 Actor 模型工作原理。
摘要由CSDN通过智能技术生成

接上面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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值