001 package com.khubla.pragmatach.plugin.cluster.multicast;
002
003 import org.jgroups.JChannel;
004 import org.jgroups.Message;
005 import org.jgroups.ReceiverAdapter;
006 import org.jgroups.View;
007 import org.slf4j.Logger;
008 import org.slf4j.LoggerFactory;
009
010 import com.khubla.pragmatach.framework.api.PragmatachException;
011
012 /**
013 * The broadcast sender and reciever
014 *
015 * @author tome
016 */
017 public class JGroupsSenderReceiver extends ReceiverAdapter {
018 /**
019 * logger
020 */
021 private final Logger logger = LoggerFactory.getLogger(this.getClass());
022 /**
023 * the channel
024 */
025 private JChannel jChannel;
026 /**
027 * cluster name
028 */
029 private final static String CLUSTER_NAME = "PragmatachSesion";
030
031 public JGroupsSenderReceiver() {
032 }
033
034 /**
035 * message
036 */
037 public void receive(Message message) {
038 try {
039 MulticastMessage.deserialize(message.getBuffer());
040 } catch (final Exception e) {
041 logger.error("Exception in receive", e);
042 }
043 }
044
045 /**
046 * send message
047 */
048 public void send(MulticastMessage multicastMessage) throws PragmatachException {
049 try {
050 final Message msg = new Message();
051 msg.setBuffer(MulticastMessage.serialize(multicastMessage));
052 jChannel.send(msg);
053 } catch (final Exception e) {
054 throw new PragmatachException("Exception in send", e);
055 }
056 }
057
058 public void shutdown() {
059 if (null != jChannel) {
060 jChannel.close();
061 jChannel = null;
062 }
063 }
064
065 public void startup() throws PragmatachException {
066 try {
067 /*
068 * the channel
069 */
070 jChannel = new JChannel();
071 jChannel.setReceiver(this);
072 jChannel.connect(CLUSTER_NAME);
073 } catch (final Exception e) {
074 throw new PragmatachException("Exception in startup", e);
075 }
076 }
077
078 /**
079 * accepted
080 */
081 public void viewAccepted(View view) {
082 }
083 }