All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.internal.cache.FindDurableQueueProcessor Maven / Gradle / Ivy

/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License. See accompanying
 * LICENSE file.
 */

package com.gemstone.gemfire.internal.cache;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.gemstone.gemfire.DataSerializer;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.distributed.internal.DM;
import com.gemstone.gemfire.distributed.internal.DistributionManager;
import com.gemstone.gemfire.distributed.internal.DistributionMessage;
import com.gemstone.gemfire.distributed.internal.MessageWithReply;
import com.gemstone.gemfire.distributed.internal.PooledDistributionMessage;
import com.gemstone.gemfire.distributed.internal.ReplyException;
import com.gemstone.gemfire.distributed.internal.ReplyMessage;
import com.gemstone.gemfire.distributed.internal.ReplyProcessor21;
import com.gemstone.gemfire.distributed.internal.ServerLocation;
import com.gemstone.gemfire.distributed.internal.ServerLocator;
import com.gemstone.gemfire.internal.cache.tier.sockets.ClientProxyMembershipID;

/**
 * A processor for helping a locator find the durable queues for a given durable client id.
 * Asks each bridge server if they have the durable id and builds a list of matching servers.
 *
 * @since 5.7
 * @author Greg Passmore
 */
public class FindDurableQueueProcessor extends ReplyProcessor21 {
  
  ////////// Public static entry point /////////

  final ArrayList durableLocations = new ArrayList();

  // @todo gregp: add javadocs
  public static ArrayList sendAndFind(ServerLocator locator, ClientProxyMembershipID proxyId,
                   DM dm) {
    Set members = ((ControllerAdvisor)locator.getDistributionAdvisor()).adviseBridgeServers();
    FindDurableQueueProcessor processor = 
      new FindDurableQueueProcessor(dm,members);
      
    FindDurableQueueMessage.send(proxyId, dm, members,processor);
    try {
      processor.waitForRepliesUninterruptibly();
    } catch (ReplyException e) {
      e.handleAsUnexpected();
    }
    ArrayList locations = processor.durableLocations;
    //This will add any local queues to the list
    findLocalDurableQueues(proxyId, locations);
    return locations;
  }
  
  private static void findLocalDurableQueues(ClientProxyMembershipID proxyId, ArrayList matches) {
    Cache c = GemFireCacheImpl.getInstance();
    if(c!=null) {
      List l = c.getBridgeServers();
      if(l!=null) {
        Iterator i = l.iterator();
        while(i.hasNext()) {
          BridgeServerImpl bs = (BridgeServerImpl)i.next();
          if(bs.getAcceptor().getCacheClientNotifier().getClientProxy(proxyId)!=null) {
            ServerLocation loc = new ServerLocation(bs.getExternalAddress(),bs.getPort());
            matches.add(loc);
          }
        }
      }
    }
  }
  

  ////////////  Instance methods //////////////
  
  @Override
  public void process(DistributionMessage msg) {
    // TODO Auto-generated method stub
    if(msg instanceof FindDurableQueueReply) {
      FindDurableQueueReply reply = (FindDurableQueueReply)msg;
        synchronized(durableLocations) {
          //add me to the durable member set
          durableLocations.addAll(reply.getMatches());
        }
    }
    super.process(msg);
  }


  /** Creates a new instance of FindDurableQueueProcessor
   */
  private FindDurableQueueProcessor(DM dm,Set members) {
    super(dm, members);
  }

  
  ///////////////   Inner message classes  //////////////////
  
  public static class FindDurableQueueMessage
    extends PooledDistributionMessage implements MessageWithReply
  {
    private int processorId;
    private ClientProxyMembershipID proxyId;
    
    protected static void send(ClientProxyMembershipID proxyId,
                             DM dm,Set members, 
                             ReplyProcessor21 proc)
    {
      FindDurableQueueMessage msg = new FindDurableQueueMessage();
      msg.processorId = proc.getProcessorId();
      msg.proxyId = proxyId;
      msg.setRecipients(members);
      if (dm.getLoggerI18n().fineEnabled()) {
        dm.getLoggerI18n().fine("FindDurableQueueMessage sending " + msg + " to " + members);
      }
      dm.putOutgoing(msg);
    }
  
    @Override
    public int getProcessorId() {
      return this.processorId;
    }
    
    public ClientProxyMembershipID getProxyId() {
      return this.proxyId;
    }

    
    @Override
    protected void process(final DistributionManager dm) {
      ArrayList matches = new ArrayList();
      try {
        findLocalDurableQueues(proxyId, matches);
        
        
      } finally {
        FindDurableQueueReply reply = new FindDurableQueueReply();
        reply.setProcessorId(this.getProcessorId());
        reply.matches = matches;
        reply.setRecipient(getSender());
        if (dm.getId().equals(getSender())) {
          reply.setSender(getSender());
          reply.dmProcess(dm);
        }
        else {
          dm.putOutgoing(reply);
        }
      }

    }

    public int getDSFID() {
      return FIND_DURABLE_QUEUE;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
      // TODO Auto-generated method stub
      return super.clone();
    }

    @Override
    public void fromData(DataInput in)
    throws IOException, ClassNotFoundException {
      super.fromData(in);
      this.processorId = in.readInt();
      this.proxyId = ClientProxyMembershipID.readCanonicalized(in);
    }
    
    @Override
    public void toData(DataOutput out) throws IOException {
      super.toData(out);
      out.writeInt(this.processorId);
      DataSerializer.writeObject(this.proxyId, out);
    }
    
    @Override
    public String toString() {
      StringBuilder buff = new StringBuilder();
      buff.append("FindDurableQueueMessage (proxyId='")
        .append(this.proxyId)
        .append("' processorId=")
        .append(this.processorId)
        .append(")");
      return buff.toString();
    }
  }
  
  
  public static class FindDurableQueueReply extends ReplyMessage {
    protected ArrayList matches = null;
  
    public ArrayList getMatches() {
      return this.matches;
    }
  
    @Override
    public int getDSFID() {
      return FIND_DURABLE_QUEUE_REPLY;
    }
    
    @Override
    public void fromData(DataInput in)
      throws IOException, ClassNotFoundException {
      super.fromData(in);
      this.matches =  DataSerializer.readArrayList(in);
    }
  
    @Override
    public void toData(DataOutput out) throws IOException {
      super.toData(out);
      DataSerializer.writeArrayList(matches, out);
    }
  
    @Override
    public String toString() {
      StringBuilder buff = new StringBuilder();
      buff.append("FindDurableQueueReply (matches='")
        .append(this.matches)
        .append("' processorId=")
        .append(this.processorId)
        .append(")");
      return buff.toString();
    }
  }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy