Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
error handling refactoring: made BaseClientActor an Akka state machine
* manage the current connectivity state in BaseClientActor * AMQP1.0 and RMQ clients use the API of their superclass Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch-si.com>
- Loading branch information
Showing
23 changed files
with
1,405 additions
and
467 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
model/connectivity/src/main/java/org/eclipse/ditto/model/connectivity/ConnectionMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2017 Bosch Software Innovations GmbH. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/org/documents/epl-2.0/index.php | ||
* | ||
* Contributors: | ||
* Bosch Software Innovations GmbH - initial contribution | ||
*/ | ||
package org.eclipse.ditto.model.connectivity; | ||
|
||
import java.util.Optional; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
import org.eclipse.ditto.json.JsonFactory; | ||
import org.eclipse.ditto.json.JsonField; | ||
import org.eclipse.ditto.json.JsonFieldDefinition; | ||
import org.eclipse.ditto.json.JsonFieldSelector; | ||
import org.eclipse.ditto.json.JsonObject; | ||
import org.eclipse.ditto.model.base.json.FieldType; | ||
import org.eclipse.ditto.model.base.json.JsonSchemaVersion; | ||
import org.eclipse.ditto.model.base.json.Jsonifiable; | ||
|
||
/** | ||
* TODO TJ doc | ||
*/ | ||
@Immutable | ||
public interface ConnectionMetrics extends Jsonifiable.WithFieldSelectorAndPredicate<JsonField> { | ||
|
||
/** | ||
* | ||
* @return | ||
*/ | ||
ConnectionStatus getConnectionStatus(); | ||
|
||
/** | ||
* | ||
* @return | ||
*/ | ||
Optional<String> getConnectionStatusDetails(); | ||
|
||
|
||
/** | ||
* Returns all non hidden marked fields of this {@code Connection}. | ||
* | ||
* @return a JSON object representation of this Connection including only non hidden marked fields. | ||
*/ | ||
@Override | ||
default JsonObject toJson() { | ||
return toJson(FieldType.notHidden()); | ||
} | ||
|
||
@Override | ||
default JsonObject toJson(final JsonSchemaVersion schemaVersion, final JsonFieldSelector fieldSelector) { | ||
return toJson(schemaVersion, FieldType.notHidden()).get(fieldSelector); | ||
} | ||
|
||
/** | ||
* An enumeration of the known {@code JsonField}s of a {@code ConnectionMetrics}. | ||
*/ | ||
@Immutable | ||
final class JsonFields { | ||
|
||
/** | ||
* JSON field containing the {@code ConnectionStatus} identifier. | ||
*/ | ||
public static final JsonFieldDefinition<String> CONNECTION_STATUS = | ||
JsonFactory.newStringFieldDefinition("connectionStatus", FieldType.REGULAR, JsonSchemaVersion.V_1, | ||
JsonSchemaVersion.V_2); | ||
|
||
/** | ||
* JSON field containing the {@code ConnectionStatus} identifier. | ||
*/ | ||
public static final JsonFieldDefinition<String> CONNECTION_STATUS_DETAILS = | ||
JsonFactory.newStringFieldDefinition("connectionStatusDetails", FieldType.REGULAR, JsonSchemaVersion.V_1, | ||
JsonSchemaVersion.V_2); | ||
|
||
private JsonFields() { | ||
throw new AssertionError(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...tivity/src/main/java/org/eclipse/ditto/model/connectivity/ImmutableConnectionMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright (c) 2017 Bosch Software Innovations GmbH. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/org/documents/epl-2.0/index.php | ||
* | ||
* Contributors: | ||
* Bosch Software Innovations GmbH - initial contribution | ||
*/ | ||
package org.eclipse.ditto.model.connectivity; | ||
|
||
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
|
||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.Immutable; | ||
|
||
import org.eclipse.ditto.json.JsonFactory; | ||
import org.eclipse.ditto.json.JsonField; | ||
import org.eclipse.ditto.json.JsonObject; | ||
import org.eclipse.ditto.json.JsonObjectBuilder; | ||
import org.eclipse.ditto.model.base.json.JsonSchemaVersion; | ||
|
||
/** | ||
* TODO TJ doc | ||
*/ | ||
@Immutable | ||
final class ImmutableConnectionMetrics implements ConnectionMetrics { | ||
|
||
private final ConnectionStatus connectionStatus; | ||
@Nullable private final String connectionStatusDetails; | ||
|
||
private ImmutableConnectionMetrics(final ConnectionStatus connectionStatus, | ||
@Nullable final String connectionStatusDetails) { | ||
|
||
this.connectionStatus = connectionStatus; | ||
this.connectionStatusDetails = connectionStatusDetails; | ||
} | ||
|
||
/** | ||
* Creates a new {@code ImmutableConnectionMetrics} instance. | ||
* | ||
* @param connectionStatus the ConnectionStatus of the metrics to create | ||
* @param connectionStatusDetails the optional details about the connection status | ||
* @return a new instance of ConnectionMetrics. | ||
*/ | ||
public static ImmutableConnectionMetrics of(final ConnectionStatus connectionStatus, | ||
@Nullable final String connectionStatusDetails) { | ||
checkNotNull(connectionStatus, "connectionStatus"); | ||
checkNotNull(connectionStatusDetails, "connectionStatusDetails"); | ||
|
||
return new ImmutableConnectionMetrics(connectionStatus, connectionStatusDetails); | ||
} | ||
|
||
/** | ||
* Creates a new {@code ConnectionMetrics} object from the specified JSON object. | ||
* | ||
* @param jsonObject a JSON object which provides the data for the ConnectionMetrics to be created. | ||
* @return a new ConnectionMetrics which is initialised with the extracted data from {@code jsonObject}. | ||
* @throws NullPointerException if {@code jsonObject} is {@code null}. | ||
* @throws org.eclipse.ditto.json.JsonParseException if {@code jsonObject} is not an appropriate JSON object. | ||
*/ | ||
public static ConnectionMetrics fromJson(final JsonObject jsonObject) { | ||
final ConnectionStatus readConnectionStatus = ConnectionStatus.forName( | ||
jsonObject.getValueOrThrow(JsonFields.CONNECTION_STATUS)).orElse(ConnectionStatus.UNKNOWN); | ||
final String readConnectionStatusDetails = jsonObject.getValueOrThrow(JsonFields.CONNECTION_STATUS_DETAILS); | ||
|
||
return of(readConnectionStatus, readConnectionStatusDetails); | ||
} | ||
|
||
@Override | ||
public ConnectionStatus getConnectionStatus() { | ||
return connectionStatus; | ||
} | ||
|
||
@Override | ||
public Optional<String> getConnectionStatusDetails() { | ||
return Optional.ofNullable(connectionStatusDetails); | ||
} | ||
|
||
@Override | ||
public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) { | ||
final Predicate<JsonField> predicate = schemaVersion.and(thePredicate); | ||
final JsonObjectBuilder jsonObjectBuilder = JsonFactory.newObjectBuilder(); | ||
jsonObjectBuilder.set(JsonFields.CONNECTION_STATUS, connectionStatus.getName(), predicate); | ||
if (connectionStatusDetails != null) { | ||
jsonObjectBuilder.set(JsonFields.CONNECTION_STATUS_DETAILS, connectionStatusDetails, predicate); | ||
} | ||
return jsonObjectBuilder.build(); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) {return true;} | ||
if (!(o instanceof ImmutableConnectionMetrics)) {return false;} | ||
final ImmutableConnectionMetrics that = (ImmutableConnectionMetrics) o; | ||
return connectionStatus == that.connectionStatus && | ||
Objects.equals(connectionStatusDetails, that.connectionStatusDetails); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(connectionStatus, connectionStatusDetails); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + " [" + | ||
"connectionStatus=" + connectionStatus + | ||
", connectionStatusDetails=" + connectionStatusDetails + | ||
"]"; | ||
} | ||
|
||
} |
Oops, something went wrong.