messing with jedis subpub
This commit is contained in:
parent
8ed7f34ab6
commit
befc2cff7c
@ -3,6 +3,7 @@
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/Classpath.Dummy/Classpath.Dummy.iml" filepath="$PROJECT_DIR$/Classpath.Dummy/Classpath.Dummy.iml" group="Core" />
|
||||
<module fileurl="file://$PROJECT_DIR$/JedisTest/JedisTest.iml" filepath="$PROJECT_DIR$/JedisTest/JedisTest.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/Mineplex.Bungee.Mineplexer/Mineplex.Bungee.Mineplexer.iml" filepath="$PROJECT_DIR$/Mineplex.Bungee.Mineplexer/Mineplex.Bungee.Mineplexer.iml" group="Bungee" />
|
||||
<module fileurl="file://$PROJECT_DIR$/Mineplex.Core/Mineplex.Core.iml" filepath="$PROJECT_DIR$/Mineplex.Core/Mineplex.Core.iml" group="Core" />
|
||||
<module fileurl="file://$PROJECT_DIR$/Mineplex.Core.Common/Mineplex.Core.Common.iml" filepath="$PROJECT_DIR$/Mineplex.Core.Common/Mineplex.Core.Common.iml" group="Core" />
|
||||
|
13
Plugins/JedisTest/JedisTest.iml
Normal file
13
Plugins/JedisTest/JedisTest.iml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="jedis" level="project" />
|
||||
<orderEntry type="library" name="commons-pool2" level="project" />
|
||||
</component>
|
||||
</module>
|
93
Plugins/JedisTest/src/ca/phinary/jedistest/Main.java
Normal file
93
Plugins/JedisTest/src/ca/phinary/jedistest/Main.java
Normal file
@ -0,0 +1,93 @@
|
||||
package ca.phinary.jedistest;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.awt.event.WindowListener;
|
||||
|
||||
import ca.phinary.jedistest.gui.ChatFrame;
|
||||
import ca.phinary.jedistest.model.JedisChat;
|
||||
|
||||
public class Main
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
SwingUtilities.invokeLater(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (InstantiationException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (IllegalAccessException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (UnsupportedLookAndFeelException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
ChatFrame chatFrame = new ChatFrame();
|
||||
final JedisChat jedisChat = new JedisChat(chatFrame, "phinaryTest", "10.33.53.16", 6379);
|
||||
|
||||
chatFrame.addWindowListener(new WindowListener()
|
||||
{
|
||||
@Override
|
||||
public void windowOpened(WindowEvent e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowClosed(WindowEvent e)
|
||||
{
|
||||
jedisChat.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowIconified(WindowEvent e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowDeiconified(WindowEvent e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowActivated(WindowEvent e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowDeactivated(WindowEvent e)
|
||||
{
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
8
Plugins/JedisTest/src/ca/phinary/jedistest/api/Chat.java
Normal file
8
Plugins/JedisTest/src/ca/phinary/jedistest/api/Chat.java
Normal file
@ -0,0 +1,8 @@
|
||||
package ca.phinary.jedistest.api;
|
||||
|
||||
public interface Chat
|
||||
{
|
||||
public void addListener(ChatListener chatListener);
|
||||
|
||||
public void clearListeners();
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package ca.phinary.jedistest.api;
|
||||
|
||||
public interface ChatListener
|
||||
{
|
||||
public void onChat(String message);
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package ca.phinary.jedistest.api;
|
||||
|
||||
public interface Console
|
||||
{
|
||||
public void println(String line);
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package ca.phinary.jedistest.api;
|
||||
|
||||
public interface Messenger
|
||||
{
|
||||
public Chat getChat();
|
||||
|
||||
public Console getConsole();
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package ca.phinary.jedistest.gui;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
import ca.phinary.jedistest.api.Chat;
|
||||
import ca.phinary.jedistest.api.Console;
|
||||
import ca.phinary.jedistest.api.Messenger;
|
||||
|
||||
public class ChatFrame extends JFrame implements Messenger
|
||||
{
|
||||
private ConsolePane _console;
|
||||
private ChatPane _chat;
|
||||
|
||||
public ChatFrame()
|
||||
{
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
_console = new ConsolePane();
|
||||
_chat = new ChatPane(this);
|
||||
|
||||
add(_console, BorderLayout.CENTER);
|
||||
add(_chat, BorderLayout.SOUTH);
|
||||
|
||||
setTitle("Phinary's Redis Chat");
|
||||
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
public Console getConsole()
|
||||
{
|
||||
return _console;
|
||||
}
|
||||
|
||||
public Chat getChat()
|
||||
{
|
||||
return _chat;
|
||||
}
|
||||
}
|
60
Plugins/JedisTest/src/ca/phinary/jedistest/gui/ChatPane.java
Normal file
60
Plugins/JedisTest/src/ca/phinary/jedistest/gui/ChatPane.java
Normal file
@ -0,0 +1,60 @@
|
||||
package ca.phinary.jedistest.gui;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.Border;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import ca.phinary.jedistest.api.Chat;
|
||||
import ca.phinary.jedistest.api.ChatListener;
|
||||
|
||||
public class ChatPane extends JPanel implements ActionListener, Chat
|
||||
{
|
||||
private ArrayList<ChatListener> _chatListeners;
|
||||
|
||||
private JTextField _textField;
|
||||
private JButton _sendButton;
|
||||
|
||||
public ChatPane(JFrame frame)
|
||||
{
|
||||
_chatListeners = new ArrayList<ChatListener>();
|
||||
|
||||
_textField = new JTextField();
|
||||
_sendButton = new JButton("Send");
|
||||
|
||||
setBorder(new EmptyBorder(5, 10, 5, 10));
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
add(_textField, BorderLayout.CENTER);
|
||||
add(_sendButton, BorderLayout.EAST);
|
||||
|
||||
_sendButton.addActionListener(this);
|
||||
frame.getRootPane().setDefaultButton(_sendButton);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e)
|
||||
{
|
||||
String text = _textField.getText();
|
||||
for (ChatListener listener : _chatListeners)
|
||||
{
|
||||
listener.onChat(text);
|
||||
}
|
||||
_textField.setText("");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(ChatListener chatListener)
|
||||
{
|
||||
_chatListeners.add(chatListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearListeners()
|
||||
{
|
||||
_chatListeners.clear();
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package ca.phinary.jedistest.gui;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.TitledBorder;
|
||||
import java.awt.*;
|
||||
|
||||
import ca.phinary.jedistest.api.Console;
|
||||
|
||||
public class ConsolePane extends JPanel implements Console
|
||||
{
|
||||
private JScrollPane _scrollPane;
|
||||
private JTextArea _textArea;
|
||||
|
||||
public ConsolePane()
|
||||
{
|
||||
_textArea = new JTextArea();
|
||||
_textArea.setEditable(false);
|
||||
_textArea.setPreferredSize(new Dimension(800, 400));
|
||||
_scrollPane = new JScrollPane(_textArea);
|
||||
_scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
setBorder(new TitledBorder("Console"));
|
||||
|
||||
add(_scrollPane, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
public synchronized void println(String line)
|
||||
{
|
||||
_textArea.append(line + "\n");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package ca.phinary.jedistest.model;
|
||||
|
||||
import ca.phinary.jedistest.api.Messenger;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
public class JedisChat
|
||||
{
|
||||
private JedisPool _jedisPool;
|
||||
private String _channel;
|
||||
|
||||
private Messenger _messenger;
|
||||
private JedisPublisher _publisher;
|
||||
|
||||
public JedisChat(Messenger messenger, final String channel, String host, int port)
|
||||
{
|
||||
_jedisPool = new JedisPool(new JedisPoolConfig(), host, port);
|
||||
_channel = channel;
|
||||
_messenger = messenger;
|
||||
|
||||
_publisher = new JedisPublisher(messenger.getConsole(), _jedisPool, channel);
|
||||
_messenger.getChat().addListener(_publisher);
|
||||
|
||||
startListen();
|
||||
}
|
||||
|
||||
public void startListen()
|
||||
{
|
||||
_messenger.getConsole().println("Attempting to connect to redis server...");
|
||||
try
|
||||
{
|
||||
final Jedis jedis = _jedisPool.getResource();
|
||||
|
||||
_messenger.getConsole().println("Successfully connected!");
|
||||
|
||||
SubscribeWorker worker = new SubscribeWorker(_messenger.getConsole(), _jedisPool, _channel);
|
||||
worker.execute();
|
||||
|
||||
} catch (Exception e)
|
||||
{
|
||||
_messenger.getConsole().println("Failed to connect to redis server!");
|
||||
}
|
||||
}
|
||||
|
||||
public void close()
|
||||
{
|
||||
System.out.println("close");
|
||||
_publisher.close();
|
||||
}
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
package ca.phinary.jedistest.model;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.net.InetAddress;
|
||||
import java.util.List;
|
||||
|
||||
import ca.phinary.jedistest.api.ChatListener;
|
||||
import ca.phinary.jedistest.api.Console;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
|
||||
public class JedisPublisher implements ChatListener
|
||||
{
|
||||
private Console _console;
|
||||
private JedisPool _jedisPool;
|
||||
private String _channelName;
|
||||
|
||||
public JedisPublisher(Console console, JedisPool jedisPool, String channelName)
|
||||
{
|
||||
_console = console;
|
||||
_jedisPool = jedisPool;
|
||||
_channelName = channelName;
|
||||
|
||||
sendConnectMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChat(String message)
|
||||
{
|
||||
String hostName = "Unknown";
|
||||
|
||||
try
|
||||
{
|
||||
hostName = InetAddress.getLocalHost().toString();
|
||||
}
|
||||
catch(Exception e) { };
|
||||
|
||||
message(hostName + " > " + message);
|
||||
}
|
||||
|
||||
private void sendConnectMessage()
|
||||
{
|
||||
String hostName = "Unknown";
|
||||
|
||||
try
|
||||
{
|
||||
hostName = InetAddress.getLocalHost().toString();
|
||||
}
|
||||
catch(Exception e) { };
|
||||
|
||||
message(hostName + " has connected to the channel");
|
||||
}
|
||||
|
||||
public void close()
|
||||
{
|
||||
String hostName = "Unknown";
|
||||
|
||||
try
|
||||
{
|
||||
hostName = InetAddress.getLocalHost().toString();
|
||||
}
|
||||
catch(Exception e) { };
|
||||
|
||||
|
||||
message(hostName + " has disconnected from the channel");
|
||||
}
|
||||
|
||||
private void message(final String text)
|
||||
{
|
||||
new SwingWorker<Void, String>()
|
||||
{
|
||||
@Override
|
||||
protected Void doInBackground() throws Exception
|
||||
{
|
||||
try
|
||||
{
|
||||
_jedisPool.getResource().publish(_channelName, text);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
publish("Failed to send message: " + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void process(List<String> chunks)
|
||||
{
|
||||
for (String s : chunks)
|
||||
_console.println(s);
|
||||
}
|
||||
}.execute();
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package ca.phinary.jedistest.model;
|
||||
|
||||
import redis.clients.jedis.JedisPubSub;
|
||||
|
||||
public class JedisSubscriber extends JedisPubSub
|
||||
{
|
||||
private SubscribeWorker _jedisWorker;
|
||||
|
||||
public JedisSubscriber(SubscribeWorker jedisWorker)
|
||||
{
|
||||
_jedisWorker = jedisWorker;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(String channel, String message)
|
||||
{
|
||||
_jedisWorker.onMessage(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPMessage(String s, String s1, String s2)
|
||||
{
|
||||
System.out.println("Pmessage:" + s + " " + s1 + " " + s2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscribe(String s, int i)
|
||||
{
|
||||
System.out.println("Subcribe: s " + i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUnsubscribe(String s, int i)
|
||||
{
|
||||
System.out.println("UnSubcribe: s " + i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPUnsubscribe(String s, int i)
|
||||
{
|
||||
System.out.println("PUnSubcribe: s " + i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPSubscribe(String s, int i)
|
||||
{
|
||||
System.out.println("Subcribe: s " + i);
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package ca.phinary.jedistest.model;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import ca.phinary.jedistest.api.Console;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
|
||||
public class SubscribeWorker extends SwingWorker<Void, String>
|
||||
{
|
||||
private Console _console;
|
||||
private JedisPool _jedisPool;
|
||||
private JedisSubscriber _jedisSubscriber;
|
||||
private String _channel;
|
||||
|
||||
public SubscribeWorker(Console console, JedisPool jedisPool, String channel)
|
||||
{
|
||||
_console = console;
|
||||
_jedisPool = jedisPool;
|
||||
_jedisSubscriber = new JedisSubscriber(this);
|
||||
_channel = channel;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground() throws Exception
|
||||
{
|
||||
publish("Attempting to connect to channel: " + _channel);
|
||||
try
|
||||
{
|
||||
Jedis j = _jedisPool.getResource();
|
||||
publish("Successfully connected to channel!");
|
||||
_jedisPool.getResource().subscribe(_jedisSubscriber, _channel);
|
||||
} catch (Exception e)
|
||||
{
|
||||
publish("Connection to channel failed:" + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void onMessage(String s)
|
||||
{
|
||||
publish(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void process(List<String> chunks)
|
||||
{
|
||||
for (String s : chunks)
|
||||
{
|
||||
_console.println(s);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user