-
Notifications
You must be signed in to change notification settings - Fork 214
/
SafeRecovery.java
53 lines (48 loc) · 1.72 KB
/
SafeRecovery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.persistence;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.pekko.actor.AbstractActor;
import org.apache.pekko.event.DiagnosticLoggingAdapter;
import org.apache.pekko.japi.pf.ReceiveBuilder;
/**
* A wrapper of the result of {@link org.apache.pekko.japi.pf.ReceiveBuilder} that never fails.
*/
public final class SafeRecovery {
/**
* Creates a Receive for recovery such that exceptions are logged as warnings
* and not thrown. Recovery messages causing exceptions have no effect.
*
* @param log The Pekko logger to write warnings to.
* @param receiveRecover The Receive to wrap around.
*
* @return the created Receive.
*/
public static AbstractActor.Receive wrapReceive(
@Nullable final DiagnosticLoggingAdapter log,
@Nonnull final AbstractActor.Receive receiveRecover) {
return ReceiveBuilder.create().matchAny(x -> {
try {
receiveRecover.onMessage().apply(x);
} catch (final Exception error) {
if (log != null) {
log.warning("Failed to recover from the following message (it is ignored): {}", x);
}
}
}).build();
}
private SafeRecovery() {
// no-op
}
}