Java知识分享
热爱技术,分享技术

Event Driven的使用

今天我们模拟写一个简单的聊天应用程序,首先我们需要定义聊天程序的几个Event

  • User Online Event:当用户:上线时来到聊天室的Event。
  • User Offline Event:当用户下线时退出聊天室的Event。
  • User Chat Event:用户在聊天室中发送聊天信息的Event。

Chat Event

首先顶一个User对象,代表聊天室的参与者,代码如下:

package cn.hackcloud.concurrency.eda.chat;
public class User {
    private final String name;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

下面定义一个UserOnlineEvent,代表用户上线Event。代码如下:

package cn.hackcloud.concurrency.eda.chat;
public class UserOnlineEvent {
    private final User user;

    public UserOnlineEvent(User user) {
        this.user = user;
    }

    public User getUser() {
        return user;
    }
}

下面定义一个UserOfflineEvent,代表用户下线Event。代码如下:

package cn.hackcloud.concurrency.eda.chat;
public class UserOfflineEvent extends UserOnlineEvent {
    public UserOfflineEvent(User user) {
        super(user);
    }
}

下面定义一个UserChatEvent,代表用户发送了聊天信息的Event,代码如下:

package cn.hackcloud.concurrency.eda.chat;
public class UserChatEvent extends UserOnlineEvent {
    //聊天需要信息嘛
    private final String message;

    public UserChatEvent(User user, String message) {
        super(user);
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}

UserChatEvent比其他两个Event多了代表聊天内容的message属性。

Chat Channel(Handler)

所有的Handler都非常简单,只是将接收到的信息输出到控制台,由于是在多线程的环境下运行,因此我们需要继承AsyncChannel。下面定义一个UserOnlineEventChannel,主要用于处理UserOnlineEvent事件,代码如下:

package cn.hackcloud.concurrency.eda.chat;

import cn.hackcloud.concurrency.eda.AsyncChannel;
import cn.hackcloud.concurrency.eda.Event;

/**
 * 用户上线的Event,打印用户登陆日志
 */
public class UserOnlineEventChannel extends AsyncChannel {
    @Override
    protected void handle(Event message) {
        UserOnlineEvent event = (UserOnlineEvent) message;
        System.out.println("The User " + event.getUser().getName() + " is Online.");
    }
}

下面定义一个UserOnlineEventChannel,主要用于处理UserflineEvent事件,代码如下:

package cn.hackcloud.concurrency.eda.chat;

import cn.hackcloud.concurrency.eda.AsyncChannel;
import cn.hackcloud.concurrency.eda.Event;

/**
* 用户下线的Event
*/
public class UserOfflineEventChannel extends AsyncChannel {
@Override
protected void handle(Event message) {
UserOfflineEvent event = (UserOfflineEvent) message;
System.out.println("The User " + event.getUser().getName() + " is offline.");
}
}

下面定义一个UserChatEventChannel,主要用于处理UserChatEvent事件,代码如下:

package cn.hackcloud.concurrency.eda.chat;
import cn.hackcloud.concurrency.eda.AsyncChannel;
import cn.hackcloud.concurrency.eda.Event;

public class UserChatEventChannel extends AsyncChannel {
    @Override
    protected void handle(Event message) {
        UserChatEvent event = (UserChatEvent) message;
        System.out.println("The User[" + event.getUser().getName() + "] + say:" + event.getMessage());
    }
}

Chat User线程

我们定义完Event和接受Event的Channel后,现在定义-一个代表聊天室参与者的
User线程,代码如下:

package cn.hackcloud.concurrency.eda.chat;
import cn.hackcloud.concurrency.eda.AsyncEventDispatcher;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

public class UserChatThread extends Thread {
    private final User user;
    private final AsyncEventDispatcher dispatcher;

    public UserChatThread(User user, AsyncEventDispatcher dispatcher) {
        super(user.getName());
        this.user = user;
        this.dispatcher = dispatcher;
    }

    @Override
    public void run() {
        try {
            //User上线,发送Online Event
            dispatcher.dispatch(new UserOnlineEvent(user));
            for (int i = 0; i < 5; i++) {
                //发送User的聊天信息
                dispatcher.dispatch(new UserChatEvent(user, getName() + "-Hello-" + i));
                //短暂休眠1~10秒
                TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(10));
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            //User下线,发送offline Event
            dispatcher.dispatch(new UserOfflineEvent(user));
        }

    }
}

当User线程启动的时候,首先发送Online Event,然后发送五条聊天信息,之后下线,在下线的时候发送Offline Event,下面写一个简单的程序测试下:

public class UserChatApplication {
public static void main(String[] args) {
//定义异步 的Router
final AsyncEventDispatcher dispatcher = new AsyncEventDispatcher();
//为Router注册Channel和Event之间的关系
dispatcher.registerChannel(UserOnlineEvent.class, new UserOnlineEventChannel());
dispatcher.registerChannel(UserOfflineEvent.class, new UserOfflineEventChannel());
dispatcher.registerChannel(UserChatEvent.class, new UserChatEventChannel());
//启动三个登录聊天室的User
new UserChatThread(new User("laishifu"), dispatcher).start();
new UserChatThread(new User("kangshifu"), dispatcher).start();
new UserChatThread(new User("xieshifu"), dispatcher).start();

}
}
Event Driven的使用插图
打赏
本站所有资源均来源于网络,仅供学习使用,请支持正版!Java技术开源 » Event Driven的使用

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

登录

找回密码

注册