View Javadoc

1   /*
2    * $Id: MulticastConnector.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.multicast;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.construct.FlowConstruct;
15  import org.mule.api.endpoint.InboundEndpoint;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.transport.udp.UdpConnector;
18  
19  /**
20   * <code>MulticastConnector</code> can dispatch mule events using ip multicasting
21   */
22  public class MulticastConnector extends UdpConnector
23  {
24  
25      public static final String MULTICAST = "multicast";
26      private boolean loopback = false;
27      private int timeToLive = INT_VALUE_NOT_SET;
28  
29      public String getProtocol()
30      {
31          return MULTICAST;
32      }
33  
34      public MulticastConnector(MuleContext context)
35      {
36          super(context);
37      }
38  
39      @Override
40      protected void doInitialise() throws InitialisationException
41      {
42          socketFactory = new MulticastSocketFactory();
43          dispatcherSocketsPool.setFactory(socketFactory);
44          dispatcherSocketsPool.setTestOnBorrow(false);
45          dispatcherSocketsPool.setTestOnReturn(true);
46          //For clarity, note that the max active value does not need to be 1 since you can have multiple
47          //Multicast sockets bound to a single port
48          //dispatcherSocketsPool.setMaxActive(1);
49      }
50  
51      public boolean isLoopback()
52      {
53          return loopback;
54      }
55  
56      public void setLoopback(boolean loopback)
57      {
58          this.loopback = loopback;
59      }
60  
61  
62      public int getTimeToLive()
63      {
64          return timeToLive;
65      }
66  
67      public void setTimeToLive(int timeToLive)
68      {
69          this.timeToLive = timeToLive;
70      }
71  
72  
73      @Override
74      protected Object getReceiverKey(FlowConstruct flowConstruct, InboundEndpoint endpoint)
75      {
76          //you can have multiple Multicast sockets bound to a single port,
77          // so store listeners with the service name too
78          return endpoint.getEndpointURI().getAddress() + "/" + flowConstruct.getName();
79      }
80  }