This is how my Supervisor looks  

public class TenantMonitorActor extends UntypedActor {

  public static final String NAME = "tenantDiskMonitor";
  private static final String assetsLocationKey = "tenant.assetsLocation";
  private static final String schedulerKey = 
"monitoring.tenant.disk.schedule.seconds";
  private static final String thresholdPercentKey = 
"monitoring.tenant.disk.threshold.percent";

  private final LoggingAdapter logging = 
Logging.getLogger(getContext().system(), this);
  private final Config config;

  private TenantMonitorActor(final Config config) {
    this.config = config;
  }

  private static final SupervisorStrategy strategy =
      new OneForOneStrategy(1, Duration.create(1, TimeUnit.SECONDS),
                            new Function<Throwable, Directive>() {

                              public Directive apply(final Throwable param) 
throws Exception {
                                if (param instanceof DiskException) {
                                  System.out.println("Disk Exception");
                                  return stop();
                                }
                                System.out.println("some other exception");
                                return restart();
                              }
                            });

  public static Props props(final Config config) {
    return Props.create(new Creator<TenantMonitorActor>(){
      public TenantMonitorActor create() throws Exception {
        return new TenantMonitorActor(config);
      }
    });
  }

  @Override
  public void onReceive(final Object message) throws Exception {
    if (message instanceof TenantMonitorMessage) {
      logging.info("Tenant Monitor Setup");
      setupDiskMonitoring();
    }
  }

  @Override
  public SupervisorStrategy supervisorStrategy() {
    return strategy;
  }



  private void setupDiskMonitoring() {
    final ActorRef diskMonitorActorRef = getDiskMonitorActorRef(config);

    final FiniteDuration start = Duration.create(0, TimeUnit.SECONDS);
    final FiniteDuration recurring = 
Duration.create(config.getInt(schedulerKey),
                                                     TimeUnit.SECONDS);

    final ActorSystem system = getContext().system();
    system.scheduler()
        .schedule(start, recurring, diskMonitorActorRef,
                  new DiskMonitorMessage(), system.dispatcher(), null);
  }

  private ActorRef getDiskMonitorActorRef(final Config monitoringConf) {
    final Props diskMonitorProps =
        DiskMonitorActor.props(new 
File(monitoringConf.getString(assetsLocationKey)),
                               monitoringConf.getLong(thresholdPercentKey));
    return getContext().actorOf(diskMonitorProps, NAME);

  }
}


This is how my Actor "throwing exception" looks

public final class DiskMonitorActor extends UntypedActor {

  private final File assetsDirectory;
  private final long thresholdPercentage;
  private final LoggingAdapter logging = 
Logging.getLogger(getContext().system(), this);

  private DiskMonitorActor(final File assetsDirectory, final long 
thresholdPercentage) {
    this.assetsDirectory = assetsDirectory;
    this.thresholdPercentage = thresholdPercentage;
  }

  public static Props props(final File assetsDirectory, final long 
thresholdPercentage) {
    return Props.create(new Creator<DiskMonitorActor>() {
      private static final long serialVersionUID = 1L;

      public DiskMonitorActor create() throws Exception {
        return new DiskMonitorActor(assetsDirectory, thresholdPercentage);
      }
    });
  }

  @Override
  public void onReceive(final Object message) throws Exception {
    if (message instanceof DiskMonitorMessage) {
      logging.info("disk: {}, total space: {}, usable space: {}",
                   assetsDirectory.getAbsolutePath(),
                   assetsDirectory.getTotalSpace(),
                   assetsDirectory.getUsableSpace());
      validateAssetsDirectory();
      if (isThresholdExceeded()) {
        logging.error("Disk full. Available Space {}", 
assetsDirectory.getFreeSpace());
        throw new DiskException("disk capacity reached threshold.");
      }

    } else {
      unhandled(message);
    }
  }

  private void validateAssetsDirectory() {
    if (!assetsDirectory.exists()) {
      System.out.println("Throwing Disk Exception");
      throw new DiskException(assetsDirectory.getAbsolutePath() + " does not 
exists");
    }
    if (!assetsDirectory.isDirectory()) {
      throw new DiskException(assetsDirectory.getAbsolutePath() + " is not a 
directory");
    }
    if (!assetsDirectory.canRead()) {
      throw new DiskException(assetsDirectory.getAbsolutePath() + " does not 
have read permission");
    }
  }

  private boolean isThresholdExceeded() {
    final long usedBytes = assetsDirectory.getTotalSpace() - 
assetsDirectory.getUsableSpace();
    final long thresholdBytes = assetsDirectory.getTotalSpace() * 
thresholdPercentage / 100;
    return usedBytes >= thresholdBytes;
  }
}


And  this is my test 

public class TenantMonitorActorTest {

  @Mock
  private File assetsDirectory;

  @Rule
  public TestName testName = new TestName();

  private final Config sourceConfig = ConfigFactory.load();

  @Before
  public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
  }

  @Test
  public void testActorForNonExistentLocation() throws Exception {
    final Map<String, String> configValues =
        Collections.singletonMap("tenant.assetsLocation", 
"/non/existentLocation");
    final Config config = mergeConfig(configValues);
    final TenantMonitorActor actor = getTenantMonitorActor(config);
    actor.onReceive(new TenantMonitorMessage());
  }

  private TenantMonitorActor getTenantMonitorActor(final Config config) {
    final ActorSystem system = ActorSystem.create("system");
    final Props props = TenantMonitorActor.props(config);
    final TestActorRef<TenantMonitorActor> actorRef =
        TestActorRef.create(system, props, testName.getMethodName());
    return actorRef.underlyingActor();
  }

  private Config mergeConfig(final Map<String, String> testValues) {
    final Config baseConfig = ConfigFactory.parseMap(testValues);
    return baseConfig.withFallback(sourceConfig);
  }
}


When I run this test, I see  

[INFO] [05/01/2015 11:38:30.950] [main] 
[akka://system/user/testActorForNonExistentLocation] Tenant Monitor Setup
Throwing Disk Exception
[INFO] [05/01/2015 11:38:30.954] [system-akka.actor.default-dispatcher-3] 
[akka://system/user/testActorForNonExistentLocation/tenantDiskMonitor] 
disk: /non/existentLocation, total space: 0, usable space: 0

Why Supervisor is not receiving exception?

Thanks

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to