use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class XmlClientConfigLocator method loadSystemPropertyFileResource.
private void loadSystemPropertyFileResource(String configSystemProperty) {
//it is a file.
File configurationFile = new File(configSystemProperty);
LOGGER.info("Using configuration file at " + configurationFile.getAbsolutePath());
if (!configurationFile.exists()) {
String msg = "Config file at '" + configurationFile.getAbsolutePath() + "' doesn't exist.";
throw new HazelcastException(msg);
}
try {
in = new FileInputStream(configurationFile);
} catch (FileNotFoundException e) {
throw new HazelcastException("Failed to open file: " + configurationFile.getAbsolutePath(), e);
}
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class DefaultAddressPicker method getPublicAddressByPortSearch.
private AddressDefinition getPublicAddressByPortSearch() throws IOException {
NetworkConfig networkConfig = config.getNetworkConfig();
boolean bindAny = hazelcastProperties.getBoolean(GroupProperty.SOCKET_SERVER_BIND_ANY);
Throwable error = null;
ServerSocket serverSocket = null;
InetSocketAddress inetSocketAddress;
boolean reuseAddress = networkConfig.isReuseAddress();
logger.finest("inet reuseAddress:" + reuseAddress);
int port = networkConfig.getPort();
// port = 0 means system will pick up an ephemeral port.
int portTrialCount = port > 0 && networkConfig.isPortAutoIncrement() ? networkConfig.getPortCount() : 1;
AddressDefinition bindAddressDef = pickAddressDef();
if (port == 0) {
logger.info("No explicit port is given, system will pick up an ephemeral port.");
}
for (int i = 0; i < portTrialCount; i++) {
/*
* Instead of reusing the ServerSocket/ServerSocketChannel, we are going to close and replace them on
* every attempt to find a free port. The reason to do this is because in some cases, when concurrent
* threads/processes try to acquire the same port, the ServerSocket gets corrupted and isn't able to
* find any free port at all (no matter if there are more than enough free ports available). We have
* seen this happening on Linux and Windows environments.
*/
serverSocketChannel = ServerSocketChannel.open();
serverSocket = serverSocketChannel.socket();
serverSocket.setReuseAddress(reuseAddress);
serverSocket.setSoTimeout(SOCKET_TIMEOUT_MILLIS);
try {
if (bindAny) {
inetSocketAddress = new InetSocketAddress(port + i);
} else {
inetSocketAddress = new InetSocketAddress(bindAddressDef.inetAddress, port + i);
}
logger.fine("Trying to bind inet socket address: " + inetSocketAddress);
serverSocket.bind(inetSocketAddress, SOCKET_BACKLOG_LENGTH);
logger.fine("Bind successful to inet socket address: " + serverSocket.getLocalSocketAddress());
break;
} catch (Exception e) {
serverSocket.close();
serverSocketChannel.close();
error = e;
}
}
if (serverSocket == null || !serverSocket.isBound()) {
String message;
if (networkConfig.isPortAutoIncrement()) {
message = "ServerSocket bind has failed. Hazelcast cannot start. config-port: " + networkConfig.getPort() + ", latest-port: " + (port + portTrialCount);
} else {
message = "Port [" + port + "] is already in use and auto-increment is disabled." + " Hazelcast cannot start.";
}
throw new HazelcastException(message, error);
}
// get the actual port that's bound by server socket
port = serverSocket.getLocalPort();
serverSocketChannel.configureBlocking(false);
bindAddress = createAddress(bindAddressDef, port);
logger.info("Picked " + bindAddress + ", using socket " + serverSocket + ", bind any local is " + bindAny);
return getPublicAddress(port);
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class IncrementCommandProcessor method handle.
@Override
public void handle(IncrementCommand incrementCommand) {
String key;
try {
key = URLDecoder.decode(incrementCommand.getKey(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new HazelcastException(e);
}
String mapName = DEFAULT_MAP_NAME;
int index = key.indexOf(':');
if (index != -1) {
mapName = MAP_NAME_PRECEDER + key.substring(0, index);
key = key.substring(index + 1);
}
try {
textCommandService.lock(mapName, key);
} catch (Exception e) {
incrementCommand.setResponse(NOT_FOUND);
if (incrementCommand.shouldReply()) {
textCommandService.sendResponse(incrementCommand);
}
return;
}
incrementUnderLock(incrementCommand, key, mapName);
textCommandService.unlock(mapName, key);
if (incrementCommand.shouldReply()) {
textCommandService.sendResponse(incrementCommand);
}
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class ObjectMultiMapProxy method aggregate.
@Override
public <SuppliedValue, Result> Result aggregate(Supplier<K, V, SuppliedValue> supplier, Aggregation<K, SuppliedValue, Result> aggregation, JobTracker jobTracker) {
try {
isNotNull(jobTracker, "jobTracker");
KeyValueSource<K, V> keyValueSource = KeyValueSource.fromMultiMap(this);
Job<K, V> job = jobTracker.newJob(keyValueSource);
Mapper mapper = aggregation.getMapper(supplier);
CombinerFactory combinerFactory = aggregation.getCombinerFactory();
ReducerFactory reducerFactory = aggregation.getReducerFactory();
Collator collator = aggregation.getCollator();
MappingJob mappingJob = job.mapper(mapper);
ReducingSubmittableJob reducingJob;
if (combinerFactory != null) {
reducingJob = mappingJob.combiner(combinerFactory).reducer(reducerFactory);
} else {
reducingJob = mappingJob.reducer(reducerFactory);
}
ICompletableFuture<Result> future = reducingJob.submit(collator);
return future.get();
} catch (Exception e) {
throw new HazelcastException(e);
}
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class IOUtil method copyDirectory.
private static void copyDirectory(File source, File target) {
if (target.exists() && !target.isDirectory()) {
throw new IllegalArgumentException("Cannot copy source directory since the target already exists " + "but it is not a directory");
}
final File targetSubDir = new File(target, source.getName());
if (!targetSubDir.exists() && !targetSubDir.mkdirs()) {
throw new HazelcastException("Could not create the target directory " + target);
}
final File[] sourceFiles = source.listFiles();
if (sourceFiles == null) {
throw new HazelcastException("Error occurred while listing directory contents for copy");
}
for (File file : sourceFiles) {
copy(file, targetSubDir);
}
}
Aggregations