1 24 25 package org.objectweb.tribe.channel; 26 27 import java.io.Serializable ; 28 import java.net.URL ; 29 import java.util.ArrayList ; 30 31 import org.jgroups.Address; 32 import org.jgroups.ChannelClosedException; 33 import org.jgroups.ChannelNotConnectedException; 34 import org.jgroups.JChannel; 35 import org.jgroups.Message; 36 import org.jgroups.TimeoutException; 37 import org.objectweb.tribe.common.Group; 38 import org.objectweb.tribe.common.GroupIdentifier; 39 import org.objectweb.tribe.exceptions.AlreadyMemberException; 40 import org.objectweb.tribe.exceptions.ChannelException; 41 import org.objectweb.tribe.exceptions.NotConnectedException; 42 import org.objectweb.tribe.messages.GroupMessage; 43 44 51 public class JGroupsReliableChannel extends ReliableGroupChannel 52 { 53 private JChannel jgroupsChannel; 55 56 59 public JGroupsReliableChannel(URL jgroupsConfigFile) throws ChannelException 60 { 61 try 62 { 63 jgroupsChannel = new JChannel(jgroupsConfigFile); 64 } 65 catch (org.jgroups.ChannelException e) 66 { 67 throw new ChannelException(e); 68 } 69 } 70 71 74 public void join(Group g) throws AlreadyMemberException, ChannelException, 75 NotConnectedException 76 { 77 if ((currentGroup != null) && currentGroup.equals(g)) 78 throw new AlreadyMemberException(); 79 if (currentGroup != null) 81 try 82 { 83 quit(); 84 } 85 catch (Exception ignore) 86 { 87 } 88 currentGroup = g; 89 try 90 { 91 jgroupsChannel.connect(g.getGroupIdentifier().getGroupName()); 92 } 93 catch (ChannelClosedException e) 94 { 95 throw new NotConnectedException(e); 96 } 97 catch (org.jgroups.ChannelException e) 98 { 99 throw new ChannelException(e); 100 } 101 } 102 103 106 public void quit() throws ChannelException, NotConnectedException 107 { 108 if (currentGroup == null) 109 throw new NotConnectedException(); 110 jgroupsChannel.close(); 111 currentGroup = null; 112 } 113 114 117 public Serializable receive() throws ChannelException, NotConnectedException 118 { 119 if (currentGroup == null) 120 throw new NotConnectedException(); 121 try 122 { 123 GroupMessage groupMessage = (GroupMessage) jgroupsChannel.receive(0); 124 if (groupMessage == null) 125 return null; 126 else 127 return groupMessage.getMessage(); 128 } 129 catch (ChannelNotConnectedException e) 130 { 131 throw new NotConnectedException(e); 132 } 133 catch (ChannelClosedException e) 134 { 135 throw new NotConnectedException(e); 136 } 137 catch (TimeoutException e) 138 { 139 throw new ChannelException( 140 "Timeout while retrieving message from channel", e); 141 } 142 } 143 144 148 public ArrayList send(Serializable msg, GroupIdentifier gid, ArrayList members) 149 throws ChannelException, NotConnectedException 150 { 151 Address dest = null; 152 Message jgroupsMsg = new Message(dest, null, new GroupMessage(msg, gid)); 153 try 154 { 155 jgroupsChannel.send(jgroupsMsg); 156 return null; 159 } 160 catch (ChannelNotConnectedException e) 161 { 162 throw new NotConnectedException(e); 163 } 164 catch (ChannelClosedException e) 165 { 166 throw new NotConnectedException(e); 167 } 168 } 169 170 173 public String toString() 174 { 175 return "JGroups channel wrapper: " + jgroupsChannel; 176 } 177 178 183 public String getProperties() 184 { 185 return jgroupsChannel.getProperties(); 186 } 187 } | Popular Tags |