/** * Connects this member to the cluster using the given <code>loadFactor</code>. The <code> * loadFactor</code> defines the (approximate) relative load that this member will receive. * * <p>A good default value is 100, which will give this member 100 nodes on the distributed hash * ring. Giving all members (proportionally) lower values will result in a less evenly distributed * hash. * * @param loadFactor The load factor for this node. * @throws ConnectionFailedException when an error occurs while connecting */ public synchronized void connect(int loadFactor) throws ConnectionFailedException { this.currentLoadFactor = loadFactor; Assert.isTrue(loadFactor >= 0, "Load Factor must be a positive integer value."); Assert.isTrue( channel.getReceiver() == null || channel.getReceiver() == messageReceiver, "The given channel already has a receiver configured. " + "Has the channel been reused with other Connectors?"); try { channel.setReceiver(messageReceiver); if (channel.isConnected() && !clusterName.equals(channel.getClusterName())) { throw new AxonConfigurationException( "The Channel that has been configured with this JGroupsConnector " + "is already connected to another cluster."); } else if (channel.isConnected()) { // we need to fetch state now that we have attached our MessageReceiver channel.getState(null, 10000); } else { // we need to connect. This will automatically fetch state as well. channel.connect(clusterName, null, 10000); } updateMembership(); } catch (Exception e) { joinedCondition.markJoined(false); channel.disconnect(); throw new ConnectionFailedException("Failed to connect to JGroupsConnectorFactoryBean", e); } }
protected void _init(JChannel ch, long sleep_time, String name) throws Exception { this.sleep_time = sleep_time; channel = ch; if (name != null) channel.setName(name); channel.connect(getClass().getSimpleName()); channel.setReceiver(receiver); try { MBeanServer server = Util.getMBeanServer(); JmxConfigurator.registerChannel( channel, server, "jgroups-" + name, channel.getClusterName(), true); } catch (Throwable ex) { System.err.println("registering the channel with JMX failed: " + ex); } }
public void testLifecycle() throws Exception { fc1 = new ForkChannel(ch, "stack", "fc1"); assert fc1.isOpen() && !fc1.isConnected() && !fc1.isClosed() : "state=" + fc1.getState(); ch.connect(CLUSTER); assert fc1.isOpen() && !fc1.isConnected() && !fc1.isClosed() : "state=" + fc1.getState(); fc1.connect("bla"); assert fc1.isOpen() && fc1.isConnected() && !fc1.isClosed() : "state=" + fc1.getState(); assert ch.getAddress().equals(fc1.getAddress()); assert ch.getClusterName().equals(fc1.getClusterName()); assert ch.getView().equals(fc1.getView()); fc1.disconnect(); assert fc1.isOpen() && !fc1.isConnected() && !fc1.isClosed() : "state=" + fc1.getState(); fc1.connect("foobar"); assert fc1.isOpen() && fc1.isConnected() && !fc1.isClosed() : "state=" + fc1.getState(); Util.close(fc1); assert !fc1.isOpen() && !fc1.isConnected() && fc1.isClosed() : "state=" + fc1.getState(); try { fc1.connect("whocares"); assert false : "a closed fork channel cannot be reconnected"; } catch (Exception ex) { assert ex instanceof IllegalStateException; } assert !fc1.isOpen() && !fc1.isConnected() && fc1.isClosed() : "state=" + fc1.getState(); Util.close(ch); assert !fc1.isOpen() && !fc1.isConnected() && fc1.isClosed() : "state=" + fc1.getState(); try { fc1.send(null, "hello"); assert false : "sending on a fork-channel with a disconnected main-channel should throw an exception"; } catch (Throwable t) { System.out.println( "got an exception (as expected) sending on a fork-channel where the main-channel is disconnected: " + t); } }
public void init(String props, String name) throws Throwable { channel = new JChannel(props); if (name != null) channel.setName(name); disp = new RpcDispatcher(channel, null, this, this); disp.setMethodLookup( new MethodLookup() { public Method findMethod(short id) { return METHODS[id]; } }); disp.setRequestMarshaller(new CustomMarshaller()); channel.connect(groupname); local_addr = channel.getAddress(); try { MBeanServer server = Util.getMBeanServer(); JmxConfigurator.registerChannel(channel, server, "jgroups", channel.getClusterName(), true); } catch (Throwable ex) { System.err.println("registering the channel in JMX failed: " + ex); } if (members.size() < 2) return; Address coord = members.get(0); ConfigOptions config = (ConfigOptions) disp.callRemoteMethod( coord, new MethodCall(GET_CONFIG), new RequestOptions(ResponseMode.GET_ALL, 5000)); if (config != null) { this.oob = config.oob; this.sync = config.sync; this.num_threads = config.num_threads; this.num_msgs = config.num_msgs; this.msg_size = config.msg_size; this.anycast_count = config.anycast_count; this.read_percentage = config.read_percentage; System.out.println("Fetched config from " + coord + ": " + config); } else System.err.println("failed to fetch config from " + coord); }