Skip to content

Commit

Permalink
Nodes evidence
Browse files Browse the repository at this point in the history
Issue: OHFJIRA-30
  • Loading branch information
roman-havlicek committed Mar 25, 2017
1 parent 0237ab9 commit 1040813
Show file tree
Hide file tree
Showing 34 changed files with 1,558 additions and 169 deletions.
Expand Up @@ -147,6 +147,11 @@ public class CoreProps {
*/
public static final String PROPERTY_INCLUDE_PATTERN = PREFIX + "dbProperty.includePattern";

/**
* Code of actual node for this application server instance.
*/
public static final String CLUSTER_ACTUAL_NODE_INSTANCE_CODE = PREFIX + "cluster.actualNodeInstance.code";

private CoreProps() {
}
}
@@ -0,0 +1,70 @@
package org.openhubframework.openhub.api.entity;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

/**
* Abstract node with base methods for get {@link NodeState} information.
*
* @author Roman Havlicek
* @see Node
* @see MutableNode
* @since 2.0
*/
public abstract class AbstractNode implements Node {

@Override
public boolean isStopped() {
return getState().equals(NodeState.STOPPED);
}

@Override
public boolean isAbleToHandleNewMessages() {
return getState().isAbleToHandleNewMessages();
}

@Override
public boolean isAbleToHandleExistingMessages() {
return getState().isAbleToHandleExistingMessages();
}

//--------------------------------------------- TOSTRING / HASH / EQUALS -------------------------------------------

@Override
public boolean equals(Object o) {
if (this == o) return true;

if (!(o instanceof Node)) return false;

Node node = (Node) o;

return new EqualsBuilder()
.append(getNodeId(), node.getNodeId())
.isEquals();
}

@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(getNodeId())
.toHashCode();
}

@Override
public String toString() {
return new ToStringBuilder(this)
.append("nodeId", getNodeId())
.append("code", getCode())
.append("name", getName())
.append("description", StringUtils.substring(getDescription(), 100))
.append("state", getState())
.toString();
}

@Override
public String toHumanString() {
return "(code = " + getCode() + ")";
}
}
Expand Up @@ -164,6 +164,8 @@ public class Message implements HumanReadable {
@Transient
private int processingPriority;

@Column(name = "node_id", nullable = true, insertable = false, updatable = false)
private Long nodeId;

/**
* Empty (default) constructor.
Expand Down Expand Up @@ -766,6 +768,25 @@ public void setProcessingPriority(int processingPriority) {
this.processingPriority = processingPriority;
}

/**
* Gets identifier of node that process this message.
*
* @return node identifier, {@code NULL} no node process this message
*/
@Nullable
public Long getNodeId() {
return nodeId;
}

/**
* Sets identifier of node that process this message.
*
* @param nodeId node identifier, {@code NULL} no node process this message
*/
public void setNodeId(@Nullable Long nodeId) {
this.nodeId = nodeId;
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
Expand Down Expand Up @@ -817,6 +838,7 @@ public String toString() {
.append("guaranteedOrder", guaranteedOrder)
.append("excludeFailedState", excludeFailedState)
.append("processingPriority", processingPriority)
.append("nodeId", nodeId)
.toString();
}

Expand Down
@@ -0,0 +1,190 @@
package org.openhubframework.openhub.api.entity;

import javax.annotation.Nullable;
import javax.persistence.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

/**
* Contains information about one node in cluster.
* Attributes in this entity can be changed.
*
* @author Roman Havlicek
* @see Node
* @since 2.0
*/
@Entity
@Table(name = "node",
uniqueConstraints = {
@UniqueConstraint(name = "uq_node_code", columnNames = {"code"}),
@UniqueConstraint(name = "uq_node_name", columnNames = {"name"})})
public class MutableNode extends AbstractNode {

private static final Logger LOG = LoggerFactory.getLogger(Node.class);

/**
* Identifier.
*/
@Id
@Column(name = "node_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long nodeId;

/**
* Unique node code.
*/
@Column(name = "code", length = 64, nullable = false, unique = true)
private String code;

/**
* Unique node name.
*/
@Column(name = "name", length = 256, nullable = false, unique = true)
private String name;

/**
* Description.
*/
@Column(name = "description", length = 2056, nullable = true)
private String description;

/**
* Node state.
*/
@Enumerated(EnumType.STRING)
@Column(name = "state", length = 64, nullable = false)
private NodeState state;

/**
* New instance only for JPA.
*/
protected MutableNode() {
}

/**
* New instance with {@link NodeState#RUN} state.
*
* @param code code
* @param name name
*/
public MutableNode(String code, String name) {
this(code, name, NodeState.RUN);
}

/**
* New instance.
*
* @param code code
* @param name name
* @param state state
*/
public MutableNode(String code, String name, NodeState state) {
Assert.hasText(code, "code must not be empty");
Assert.hasText(name, "name must not be empty");
Assert.notNull(state, "state must not be null");

this.code = code;
this.name = name;
this.state = state;
}

//-------------------------------------------------- STATE ---------------------------------------------------------

/**
* Node handles all messages (existing and new) {@link NodeState#RUN}.
*/
public void setRunState() {
setState(NodeState.RUN);
}

/**
* Node handles only existing messages (new message will be rejected)
* {@link NodeState#HANDLES_EXISTING_MESSAGES}.
*/
public void setHandleOnlyExistingMessageState() {
setState(NodeState.HANDLES_EXISTING_MESSAGES);
}

/**
* Node is stopped (new message will be rejected and existing is not processing)
* {@link NodeState#STOPPED}.
*/
public void setStoppedState() {
setState(NodeState.STOPPED);
}

//--------------------------------------------------- SET / GET ----------------------------------------------------

@Override
@Nullable
public Long getNodeId() {
return nodeId;
}

@Override
public String getCode() {
return code;
}

/**
* Sets code of this node.
*
* @param code code
*/
public void setCode(String code) {
Assert.hasText(code, "code must not be empty");

this.code = code;
}

@Override
public String getName() {
return name;
}

/**
* Sets name ot this node.
*
* @param name name
*/
public void setName(String name) {
Assert.hasText(name, "name must not be empty");

this.name = name;
}

@Override
@Nullable
public String getDescription() {
return description;
}

/**
* Sets description
*
* @param description description, {@code NULL} - node has no description
*/
public void setDescription(@Nullable String description) {
this.description = description;
}

@Override
public NodeState getState() {
return state;
}

/**
* Sets state of this node.
*
* @param state state
*/
private void setState(NodeState state) {
Assert.notNull(state, "state must not be null");

this.state = state;

LOG.info("State of node {} was changed to {}", toHumanString(), getState());
}
}

0 comments on commit 1040813

Please sign in to comment.