Package eu.mosaic_cloud.sunutilities

Source Code of eu.mosaic_cloud.sunutilities.Countcharacters$CountcharactersContext

//###
//# #%L
//# mosaic-mjades
//# %%
//# Copyright (C) 2010 - 2012 Seconda Università di Napoli
//#Authors: Massimiliano Rak, Giuseppe Aversano
//# %%
//# 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.
//# #L%
//###
package eu.mosaic_cloud.sunutilities;


import eu.mosaic_cloud.cloudlets.connectors.kvstore.IKvStoreConnector;
import eu.mosaic_cloud.cloudlets.connectors.kvstore.IKvStoreConnectorFactory;
import eu.mosaic_cloud.cloudlets.connectors.kvstore.KvStoreCallbackCompletionArguments;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.AmqpQueueConsumeCallbackArguments;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueueConsumerConnector;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueueConsumerConnectorFactory;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueuePublisherConnector;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueuePublisherConnectorFactory;
import eu.mosaic_cloud.cloudlets.core.CallbackArguments;
import eu.mosaic_cloud.cloudlets.core.CloudletCallbackArguments;
import eu.mosaic_cloud.cloudlets.core.CloudletCallbackCompletionArguments;
import eu.mosaic_cloud.cloudlets.core.GenericCallbackCompletionArguments;
import eu.mosaic_cloud.cloudlets.core.ICallback;
import eu.mosaic_cloud.cloudlets.core.ICloudletController;
import eu.mosaic_cloud.cloudlets.tools.DefaultAmqpPublisherConnectorCallback;
import eu.mosaic_cloud.cloudlets.tools.DefaultAmqpQueueConsumerConnectorCallback;
import eu.mosaic_cloud.cloudlets.tools.DefaultCloudletCallback;
import eu.mosaic_cloud.cloudlets.tools.DefaultKvStoreConnectorCallback;
import eu.mosaic_cloud.platform.core.configuration.ConfigurationIdentifier;
import eu.mosaic_cloud.platform.core.configuration.IConfiguration;
import eu.mosaic_cloud.platform.core.utils.PlainTextDataEncoder;
import eu.mosaic_cloud.platform.core.utils.SerializedDataEncoder;
import eu.mosaic_cloud.tools.callbacks.core.CallbackCompletion;
import eu.mosaic_cloud.platform.core.utils.JsonDataEncoder;
import java.util.Vector;


public class Countcharacters
{
 
  // Consumer dal client
  public static final class AmqpConsumerCallback
      extends DefaultAmqpQueueConsumerConnectorCallback<CountcharactersContext, String, Void>
  {
    @Override
    public CallbackCompletion<Void> consume (final CountcharactersContext context, final AmqpQueueConsumeCallbackArguments<String> arguments)
    {
      final String data = arguments.getMessage ();
     
      this.logger.info("\nDato ricevuto da Countcharacters:"+data+"\n");
     
      int count=data.length() / 10;
      String CountString = Integer.toString (count);
     
      this.logger.info("\n Numero caratteri contati\n"+count);
      this.logger.info("\nPubblicazione Numero elementi contati\n"+CountString);
     
      //this.logger.info("\nPubblicazione Numero elementi contati\n");
     
      context.publisher.publish(CountString, null);
     
      context.consumer.acknowledge (arguments.getToken ());
     
     
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> destroySucceeded (final CountcharactersContext context, final CallbackArguments arguments)
    {
      this.logger.info ("Countcharacters  destroyed successfully.");
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> initializeSucceeded (final CountcharactersContext context, final CallbackArguments arguments)
    {
      this.logger.info ("Countcharacters ConsumerClient initialized successfully.");
      return ICallback.SUCCESS;
    }
   
    public CallbackCompletion<Void> acknowledgeSucceeded (final CountcharactersContext context, final GenericCallbackCompletionArguments<Void> arguments)
    {
    //  context.cloudlet.destroy ();
      return ICallback.SUCCESS;
    }
  }
 
 
 
 
  //Push verso il WG
 
  public static final class AmqpPublisherCallback
  extends DefaultAmqpPublisherConnectorCallback<CountcharactersContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final CountcharactersContext context, final CallbackArguments arguments)
{
  this.logger.info ("Countcharacters publisher  destroyed successfully.");
  return ICallback.SUCCESS;
}

@Override
public CallbackCompletion<Void> initializeSucceeded (final CountcharactersContext context, final CallbackArguments arguments)
{
  this.logger.info ("Countcharacters publisher  initialized successfully.");
  return ICallback.SUCCESS;
}

public CallbackCompletion<Void> publishSucceeded (final CountcharactersContext context, final GenericCallbackCompletionArguments<String> arguments)
{
  this.logger.info ("Countcharacters  publisher push successfully");
  this.logger.info("\nPublisher Valutazione OK\n");
  //context.cloudlet.destroy ();
  return ICallback.SUCCESS;
}
}
 
 
 
 
 
 
 
 
 
  public static final class LifeCycleHandler
      extends DefaultCloudletCallback<CountcharactersContext>
  {
    @Override
    public CallbackCompletion<Void> destroy (final CountcharactersContext context, final CloudletCallbackArguments<CountcharactersContext> arguments)
    {
      this.logger.info ("Countcharacters destroying...");
      return CallbackCompletion.createAndChained (context.consumer.destroy (),context.publisher.destroy());
    }
   
    @Override
    public CallbackCompletion<Void> destroySucceeded (final CountcharactersContext context, final CloudletCallbackCompletionArguments<CountcharactersContext> arguments)
    {
      this.logger.info ("Countcharacters destroyed successfully.");
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> initialize (final CountcharactersContext context, final CloudletCallbackArguments<CountcharactersContext> arguments)
    {
      this.logger.info ("Countcharacters initializing...");
      context.cloudlet = arguments.getCloudlet ();
      final IConfiguration configuration = context.cloudlet.getConfiguration ();
      final IConfiguration queueConfiguration2 = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queue1.consumer"));
      context.consumer = context.cloudlet.getConnectorFactory (IAmqpQueueConsumerConnectorFactory.class).create (queueConfiguration2, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpConsumerCallback (), context);
      final IConfiguration queueConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queue2.publisher"));
      context.publisher = context.cloudlet.getConnectorFactory (IAmqpQueuePublisherConnectorFactory.class).create (queueConfiguration, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpPublisherCallback (), context);
      return CallbackCompletion.createAndChained (context.consumer.initialize (),context.publisher.initialize ());
    }
   
    @Override
    public CallbackCompletion<Void> initializeSucceeded (final CountcharactersContext context, final CloudletCallbackCompletionArguments<CountcharactersContext> arguments)
    {
     
      this.logger.info ("Countcharacters initialized successfully.");
      return ICallback.SUCCESS;
    }
  }
 
  public static final class CountcharactersContext
  {
    ICloudletController<CountcharactersContext> cloudlet;
    IAmqpQueueConsumerConnector<String, Void> consumer;
    IAmqpQueuePublisherConnector<String, Void> publisher;
   
  }
}
TOP

Related Classes of eu.mosaic_cloud.sunutilities.Countcharacters$CountcharactersContext

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.