JGroups實現(xiàn)聊天小程序
更新時間:2018年07月23日 15:50:20 作者:java_派大星
這篇文章主要為大家詳細介紹了JGroups實現(xiàn)聊天小程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了JGroups實現(xiàn)聊天小程序的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
代碼部分:
package com.lei.jgoups; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.LinkedList; import java.util.List; import org.jgroups.JChannel; import org.jgroups.Message; import org.jgroups.ReceiverAdapter; import org.jgroups.View; import org.jgroups.util.Util; public class SimpleChat extends ReceiverAdapter{ JChannel channel; String user_name=System.getProperty("user.name", "n/a"); final List<String> state=new LinkedList<String>(); public static void main(String[] args) throws Exception { new SimpleChat().start(); } private void start() throws Exception { channel=new JChannel();// 使用默認的配置, udp.xml【YBXIANG:】該文件位于jgroups-x.y.z.Final.jar中。 channel.setReceiver(this);//注冊一個 Receiver 來接收消息并查看變化 channel.connect("ChatCluster"); channel.getState(null, 10000); eventLoop(); channel.close(); } private void eventLoop() { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while(true) { try { System.out.print(">"); System.out.flush(); String line=in.readLine().toLowerCase(); if(line.startsWith("quit") || line.startsWith("exit")) break; line="[" + user_name + "] " + line; Message msg=new Message(null, line); channel.send(msg); } catch(Exception e) { } } } //如果有節(jié)點加入后會回調(diào)此函數(shù) public void viewAccepted(View new_view) { System.out.println("** view: " + new_view); } //接收到消息后會調(diào)用此函數(shù) public void receive(Message msg) { String line=msg.getSrc() + ": " + msg.getObject(); System.out.println(line); synchronized(state) {//同步調(diào)用 state.add(line); } } //getState回調(diào)方法 public void getState(OutputStream output) throws Exception { synchronized(state) { Util.objectToStream(state, new DataOutputStream(output)); } } // 從input stream中讀取狀態(tài),然后做相應的設置: public void setState(InputStream input) throws Exception { List<String> list; list=(List<String>)Util.objectFromStream(new DataInputStream(input)); synchronized(state) { state.clear(); state.addAll(list); } System.out.println(list.size() + " messages in chat history):"); for(String str: list) { System.out.println(str); } } }
架包:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 重載、重寫、構(gòu)造函數(shù)的實例詳解
這篇文章主要介紹了Java 重載、重寫、構(gòu)造函數(shù)的實例詳解的相關(guān)資料,希望通過本文大家能理解掌握java 面向?qū)ο蟮姆椒?,需要的朋友可以參考?/div> 2017-09-09詳細分析java并發(fā)之volatile關(guān)鍵字
這篇文章主要介紹了java并發(fā)之volatile關(guān)鍵字的的相關(guān)資料,文中代碼非常詳細,幫助大家更好的理解和學習,感興趣的朋友可以了解下2020-06-06SpringCloud之分布式配置中心Spring Cloud Config高可用配置實例代碼
這篇文章主要介紹了SpringCloud之分布式配置中心Spring Cloud Config高可用配置實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04java中判斷字段真實長度的實例(中文2個字符,英文1個字符)
下面小編就為大家?guī)硪黄猨ava中判斷字段真實長度的實例(中文2個字符,英文1個字符)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01java編程中拷貝數(shù)組的方式及相關(guān)問題分析
這篇文章主要介紹了java編程中拷貝數(shù)組的方式及相關(guān)問題分析,分享了Java中數(shù)組復制的四種方式,其次對二維數(shù)組的簡單使用有一段代碼示例,具有一定參考價值,需要的朋友可以了解下。2017-11-11SpringBoot Admin健康檢查功能的實現(xiàn)
admin主要就是告訴運維人員,服務出現(xiàn)異常,然后進行通知(微信、郵件、短信、釘釘?shù)龋┛梢苑浅?焖偻ㄖ竭\維人員,相當報警功能,接下來通過本文給大家介紹SpringBoot Admin健康檢查的相關(guān)知識,一起看看吧2021-06-06最新評論