package com.example.jgroupsdemo;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.jgroups.*;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class HelloJGroups implements Receiver {
/** 用户名称 */
private String _userName = null;
/** JChannel */
private JChannel _channel = null;
/**
* 开始测试
*/
@PostConstruct
private void start() throws Exception {
// 在这里生成用户名, User.00
this._userName = "User." + (int)(Math.random() * 100);
// 建立 JChannel
this._channel = new JChannel();
this._channel.setReceiver(this);
this._channel.connect("ChatCluster");
// 事件循环
this.eventLoop();
// 事件循环结束以后,
// 关闭 JChannel
this._channel.close();
}
/**
* 事件循环, 从终端读取文字
*/
private void eventLoop() {
try {
// 建立并发送消息,
// 消息内容是 "${userName} : ${ln}"
Message msg=new ObjectMessage(null, this._userName + " hello world");
this._channel.send(msg);
} catch (Exception ex) {
// 输出错误日志
ex.printStackTrace();
}
}
@Override
public void viewAccepted(View v) {
log.info("viewAccepted : " + v);
}
@Override
public void receive(Message msg) {
log.info("address is {}, info is {}",msg.getSrc(),msg.getObject());
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)