Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
<url>http://animesh.org/</url>
<timezone>Asia/Kolkata</timezone>
</contributor>
<contributor>
<name>Christian Oestreich</name>
<url>https://github.com/ctoestreich</url>
</contributor>
<contributor>
<name>David Parkinson</name>
<url>https://github.com/dparkinson</url>
Expand Down
84 changes: 61 additions & 23 deletions src/main/java/net/greghaines/jesque/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/
package net.greghaines.jesque;

import java.io.Serializable;

import net.greghaines.jesque.utils.JesqueUtils;

import java.io.Serializable;
import java.util.Set;

/**
* An immutable configuration bean for use with the rest of the project.
*
Expand All @@ -35,26 +36,21 @@ public class Config implements Serializable {
private final String password;
private final String namespace;
private final int database;
private final Set<String> sentinels;
private final String masterName;

/**
* Using a ConfigBuilder is recommended...
*
* @param host
* the Reds hostname
* @param port
* the Redis port number
* @param timeout
* the Redis connection timeout
* @param password
* the Redis database password
* @param namespace
* the Redis namespace to prefix keys with
* @param database
* the Redis database to use
* @param host the Reds hostname
* @param port the Redis port number
* @param timeout the Redis connection timeout
* @param password the Redis database password
* @param namespace the Redis namespace to prefix keys with
* @param database the Redis database to use
* @see ConfigBuilder
*/
public Config(final String host, final int port, final int timeout, final String password, final String namespace,
final int database) {
public Config(final String host, final int port, final int timeout, final String password, final String namespace, final int database) {
if (host == null || "".equals(host)) {
throw new IllegalArgumentException("host must not be null or empty: " + host);
}
Expand All @@ -76,6 +72,45 @@ public Config(final String host, final int port, final int timeout, final String
this.password = password;
this.namespace = namespace;
this.database = database;
this.sentinels = null;
this.masterName = null;
}

/**
* Using a ConfigBuilder is recommended...
*
* @param sentinels the Redis set of sentinels
* @param masterName the Redis master name
* @param timeout the Redis connection timeout
* @param password the Redis database password
* @param namespace the Redis namespace to prefix keys with
* @param database the Redis database to use
* @see ConfigBuilder
*/
public Config(final Set<String> sentinels, final String masterName, final int timeout, final String password, final String namespace, final int database) {
if (sentinels == null || sentinels.size() < 1) {
throw new IllegalArgumentException("sentinels must not be null or empty: " + sentinels);
}
if (masterName == null || "".equals(masterName)) {
throw new IllegalArgumentException("master must not be null or empty: " + masterName);
}
if (timeout < 0) {
throw new IllegalArgumentException("timeout must not be negative: " + timeout);
}
if (namespace == null) {
throw new IllegalArgumentException("namespace must not be null");
}
if (database < 0) {
throw new IllegalArgumentException("database must not be negative: " + database);
}
this.sentinels = sentinels;
this.masterName = masterName;
this.timeout = timeout;
this.password = password;
this.namespace = namespace;
this.database = database;
this.host = ConfigBuilder.DEFAULT_HOST;
this.port = ConfigBuilder.DEFAULT_PORT;
}

/**
Expand Down Expand Up @@ -120,6 +155,10 @@ public int getDatabase() {
return this.database;
}

public Set<String> getSentinels() { return this.sentinels; }

public String getMasterName() { return this.masterName; }

/**
* @return the Redis protocol URI this Config will connect to
*/
Expand All @@ -143,8 +182,10 @@ public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + this.database;
result = prime * result + ((this.host == null) ? 0 : this.host.hashCode());
result = prime * result + ((this.namespace == null) ? 0 : this.namespace.hashCode());
result = prime * result + (this.host.hashCode());
result = prime * result + (this.namespace.hashCode());
result = prime * result + ((this.sentinels == null) ? 0 : this.sentinels.hashCode());
result = prime * result + ((this.masterName == null) ? 0 : this.masterName.hashCode());
result = prime * result + this.port;
result = prime * result + this.timeout;
return result;
Expand All @@ -160,11 +201,8 @@ public boolean equals(final Object obj) {
equal = true;
} else if (obj instanceof Config) {
final Config other = (Config) obj;
equal = ((this.database == other.database)
&& (this.port == other.port)
&& (this.timeout == other.timeout)
&& JesqueUtils.nullSafeEquals(this.host, other.host)
&& JesqueUtils.nullSafeEquals(this.namespace, other.namespace));
equal = ((this.database == other.database) && (this.port == other.port) && (this.timeout == other.timeout) && JesqueUtils.nullSafeEquals(this.host, other.host)
&& JesqueUtils.nullSafeEquals(this.namespace, other.namespace)) && JesqueUtils.nullSafeEquals(this.sentinels, other.sentinels) && JesqueUtils.nullSafeEquals(this.masterName, other.masterName);
}
return equal;
}
Expand Down
105 changes: 70 additions & 35 deletions src/main/java/net/greghaines/jesque/ConfigBuilder.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
/*
* Copyright 2011 Greg Haines
*
* 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.
*/
* Copyright 2011 Greg Haines
*
* 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.
*/
package net.greghaines.jesque;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
* A fluent-style builder for {@link Config}s.
Expand All @@ -27,18 +29,22 @@ public class ConfigBuilder implements Serializable {

private static final long serialVersionUID = 730947307298353317L;

/** localhost */
//localhost
public static final String DEFAULT_HOST = "localhost";
/** 6379 */
// 6379
public static final int DEFAULT_PORT = 6379;
/** 5 seconds */
// 5 seconds
public static final int DEFAULT_TIMEOUT = 5000;
/** null */
// null
public static final String DEFAULT_PASSWORD = null;
/** All resque clients use "resque" by default */
// All resque clients use "resque" by default
public static final String DEFAULT_NAMESPACE = "resque";
/** 0 */
// 0
public static final int DEFAULT_DATABASE = 0;
// []
public static final HashSet<String> DEFAULT_SENTINELS = null;
// null
public static final String DEFAULT_MASTERNAME = null;

/**
* @return a Config with all the default values set
Expand All @@ -52,6 +58,8 @@ public static Config getDefaultConfig() {
private int timeout = DEFAULT_TIMEOUT;
private String password = DEFAULT_PASSWORD;
private String namespace = DEFAULT_NAMESPACE;
private Set<String> sentinels = DEFAULT_SENTINELS;
private String masterName = DEFAULT_MASTERNAME;
private int database = DEFAULT_DATABASE;

/**
Expand All @@ -65,8 +73,7 @@ public ConfigBuilder() {
* Create a new ConfigBuilder using an existing Config as the starting
* point.
*
* @param startingPoint
* the Config instance to copy the values from
* @param startingPoint the Config instance to copy the values from
*/
public ConfigBuilder(final Config startingPoint) {
if (startingPoint == null) {
Expand All @@ -77,13 +84,14 @@ public ConfigBuilder(final Config startingPoint) {
this.timeout = startingPoint.getTimeout();
this.namespace = startingPoint.getNamespace();
this.database = startingPoint.getDatabase();
this.sentinels = startingPoint.getSentinels();
this.masterName = startingPoint.getMasterName();
}

/**
* Configs created by this ConfigBuilder will have the given Redis hostname.
*
* @param host
* the Redis hostname
* @param host the Redis hostname
* @return this ConfigBuilder
*/
public ConfigBuilder withHost(final String host) {
Expand All @@ -98,8 +106,7 @@ public ConfigBuilder withHost(final String host) {
* Configs created by this ConfigBuilder will have the given Redis port
* number.
*
* @param port
* the Redis port number
* @param port the Redis port number
* @return this ConfigBuilder
*/
public ConfigBuilder withPort(final int port) {
Expand All @@ -114,8 +121,7 @@ public ConfigBuilder withPort(final int port) {
* Configs created by this ConfigBuilder will have the given Redis
* connection timeout.
*
* @param timeout
* the Redis connection timeout
* @param timeout the Redis connection timeout
* @return this ConfigBuilder
*/
public ConfigBuilder withTimeout(final int timeout) {
Expand All @@ -130,8 +136,7 @@ public ConfigBuilder withTimeout(final int timeout) {
* Configs created by this ConfigBuilder will authenticate with the given
* Redis password.
*
* @param password
* the Redis password
* @param password the Redis password
* @return this ConfigBuilder
*/
public ConfigBuilder withPassword(final String password) {
Expand All @@ -143,8 +148,7 @@ public ConfigBuilder withPassword(final String password) {
* Configs created by this ConfigBuilder will have the given Redis namespace
* to prefix keys with.
*
* @param namespace
* the Redis namespace to prefix keys with
* @param namespace the Redis namespace to prefix keys with
* @return this ConfigBuilder
*/
public ConfigBuilder withNamespace(final String namespace) {
Expand All @@ -158,8 +162,7 @@ public ConfigBuilder withNamespace(final String namespace) {
/**
* Configs created by this ConfigBuilder will use the given Redis database.
*
* @param database
* the Redis database to use
* @param database the Redis database to use
* @return this ConfigBuilder
*/
public ConfigBuilder withDatabase(final int database) {
Expand All @@ -170,10 +173,42 @@ public ConfigBuilder withDatabase(final int database) {
return this;
}

/**
* Configs created by this ConfigBuilder will use the given Redis sentinels.
*
* @param sentinels the Redis set of sentinels
* @return this ConfigBuilder
*/
public ConfigBuilder withSentinels(final Set<String> sentinels) {
if (sentinels == null || sentinels.size() < 1) {
throw new IllegalArgumentException("sentinels is null or empty: " + sentinels);
}
this.sentinels = sentinels;
return this;
}

/**
* Configs created by this ConfigBuilder will use the given Redis master name.
*
* @param masterName the Redis set of sentinels
* @return this ConfigBuilder
*/
public ConfigBuilder withMasterName(final String masterName) {
if (masterName == null || "".equals(masterName)) {
throw new IllegalArgumentException("masterName is null or empty: " + masterName);
}
this.masterName = masterName;
return this;
}

/**
* @return a new Config initialized with the current values
*/
public Config build() {
return new Config(this.host, this.port, this.timeout, this.password, this.namespace, this.database);
if (this.sentinels != null && this.sentinels.size() > 0 && masterName != null && !"".equals(masterName)) {
return new Config(this.sentinels, this.masterName, this.timeout, this.password, this.namespace, this.database);
} else {
return new Config(this.host, this.port, this.timeout, this.password, this.namespace, this.database);
}
}
}
Loading