Skip to content

Commit

Permalink
Merge pull request #12 from emre-aydin/issue-10
Browse files Browse the repository at this point in the history
Use instance-name parameter to configure Hazelcast instance & add failover tests
  • Loading branch information
Mesut Celik authored Jan 9, 2017
2 parents 5f1a0ec + 2d74154 commit 57371af
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<timestamp>${maven.build.timestamp}</timestamp>
<extraVmArgs/>

<hazelcast.version>3.6.2</hazelcast.version>
<hazelcast.version>3.7.4</hazelcast.version>

<servlet.api.version>3.0.1</servlet.api.version>
<junit.version>4.11</junit.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,18 @@

import java.io.IOException;


public class P2PLifecycleListener implements LifecycleListener {


private static Config config;
private String configLocation;

@Override
public void lifecycleEvent(LifecycleEvent event) {

String shutdown = System.getProperty("hazelcast.tomcat.shutdown_hazelcast_instance");
if (getConfigLocation() == null) {
setConfigLocation("hazelcast-default.xml");
}

if ("start".equals(event.getType())) {

try {
config = ConfigLoader.load(getConfigLocation());
} catch (IOException e) {
Expand All @@ -39,15 +34,16 @@ public void lifecycleEvent(LifecycleEvent event) {
if (config == null) {
throw new RuntimeException("failed to find configLocation:" + getConfigLocation());
}
config.setInstanceName(SessionManager.DEFAULT_INSTANCE_NAME);
if (config.getInstanceName() == null) {
config.setInstanceName(SessionManager.DEFAULT_INSTANCE_NAME);
}
Hazelcast.getOrCreateHazelcastInstance(config);
} else if ("stop".equals(event.getType()) && !"false".equals(shutdown)) {
HazelcastInstance instance = Hazelcast.getHazelcastInstanceByName(SessionManager.DEFAULT_INSTANCE_NAME);
if (instance != null) {
instance.shutdown();
}
}

}


Expand All @@ -62,5 +58,4 @@ public void setConfigLocation(String configLocation) {
public static Config getConfig() {
return config;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public abstract class WebContainerConfigurator<T> {
protected String mapName = "default";
protected int sessionTimeout;
protected boolean deferredWrite;
protected String configLocation = "hazelcast.xml";

public WebContainerConfigurator<T> port(int port) {
this.port = port;
Expand Down Expand Up @@ -40,6 +41,11 @@ public WebContainerConfigurator<T> deferredWrite(boolean deferredWrite) {
return this;
}

public WebContainerConfigurator<T> configLocation(String configLocation) {
this.configLocation = configLocation;
return this;
}

public abstract T configure() throws Exception;

public abstract void start() throws Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@ public abstract class AbstractP2PStickySessionsTest extends AbstractStickySessio
@Before
public void init() throws Exception {
instance1 = getWebContainerConfigurator();
instance1.port(SERVER_PORT_1).sticky(true).clientOnly(false).sessionTimeout(10).start();
instance1.port(SERVER_PORT_1)
.sticky(true)
.clientOnly(false)
.sessionTimeout(10)
.configLocation("hazelcast-1.xml")
.start();

instance2 = getWebContainerConfigurator();
instance2.port(SERVER_PORT_2).sticky(true).clientOnly(false).sessionTimeout(10).start();
instance2.port(SERVER_PORT_2)
.sticky(true)
.clientOnly(false)
.sessionTimeout(10)
.configLocation("hazelcast-2.xml")
.start();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hazelcast.session.sticky;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.session.AbstractHazelcastSessionsTest;
Expand Down Expand Up @@ -140,6 +141,25 @@ public void test_LastAccessTime() throws Exception {
assertNotEquals(lastAccessTime1, lastAccessTime2);
}

@Test(timeout = 80000)
public void testFailover() throws Exception {
CookieStore cookieStore = new BasicCookieStore();
String value = executeRequest("read", SERVER_PORT_1, cookieStore);
assertEquals("null", value);

executeRequest("write", SERVER_PORT_1, cookieStore);

instance1.stop();

HazelcastInstance hzInstance1 = Hazelcast.getHazelcastInstanceByName("hzInstance1");
if (hzInstance1 != null) {
hzInstance1.shutdown();
}

value = executeRequest("read", SERVER_PORT_2, cookieStore);
assertEquals("value", value);
}

// @Test
// public void testCleanupAfterSessionExpire() throws Exception {
// int DEFAULT_SESSION_TIMEOUT = 10;
Expand Down
30 changes: 30 additions & 0 deletions tomcat-core/src/test/resources/hazelcast-1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
~
~ 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.
-->
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config.xsd"
xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<instance-name>hzInstance1</instance-name>
<network>
<port auto-increment="true">5701</port>
<join>
<multicast enabled="false"/>
<tcp-ip enabled="true">
<interface>127.0.0.1</interface>
</tcp-ip>
</join>
</network>
</hazelcast>
30 changes: 30 additions & 0 deletions tomcat-core/src/test/resources/hazelcast-2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
~
~ 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.
-->
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config.xsd"
xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<instance-name>hzInstance2</instance-name>
<network>
<port auto-increment="true">5701</port>
<join>
<multicast enabled="false"/>
<tcp-ip enabled="true">
<interface>127.0.0.1</interface>
</tcp-ip>
</join>
</network>
</hazelcast>
29 changes: 29 additions & 0 deletions tomcat-core/src/test/resources/hazelcast.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
~
~ 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.
-->
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config.xsd"
xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<network>
<port auto-increment="true">5701</port>
<join>
<multicast enabled="false"/>
<tcp-ip enabled="true">
<interface>127.0.0.1</interface>
</tcp-ip>
</join>
</network>
</hazelcast>
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import org.apache.catalina.Context;
import org.apache.catalina.Engine;
import org.apache.catalina.Host;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardServer;
import org.apache.catalina.realm.MemoryRealm;
import org.apache.catalina.startup.Embedded;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;

import java.io.File;
import java.net.URL;
Expand All @@ -21,6 +24,8 @@ public class Tomcat6Configurator extends WebContainerConfigurator<Embedded> {

private String appName;

private final Log log = LogFactory.getLog(Tomcat6Configurator.class);

public Tomcat6Configurator(String appName) {
this.appName = appName;
}
Expand All @@ -41,7 +46,9 @@ public Embedded configure() throws Exception {

final Embedded catalina = new Embedded(memoryRealm);
if (!clientOnly) {
catalina.addLifecycleListener(new P2PLifecycleListener());
P2PLifecycleListener listener = new P2PLifecycleListener();
listener.setConfigLocation(configLocation);
catalina.addLifecycleListener(listener);
} else {
catalina.addLifecycleListener(new ClientServerLifecycleListener());
}
Expand Down Expand Up @@ -84,7 +91,11 @@ public void start() throws Exception {

@Override
public void stop() throws Exception {
tomcat.stop();
try {
tomcat.stop();
} catch (LifecycleException e) {
log.warn("Failed to stop Tomcat. May be already stopped.");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public Tomcat configure() throws Exception {

Tomcat tomcat = new Tomcat();
if (!clientOnly) {
tomcat.getServer().addLifecycleListener(new P2PLifecycleListener());
P2PLifecycleListener listener = new P2PLifecycleListener();
listener.setConfigLocation(configLocation);
tomcat.getServer().addLifecycleListener(listener);
} else {
tomcat.getServer().addLifecycleListener(new ClientServerLifecycleListener());
}
Expand Down Expand Up @@ -70,7 +72,9 @@ public void start() throws Exception {

@Override
public void stop() throws Exception {
tomcat.stop();
if (tomcat.getServer().getState().isAvailable()) {
tomcat.stop();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public Tomcat configure() throws Exception {

Tomcat tomcat = new Tomcat();
if (!clientOnly) {
tomcat.getServer().addLifecycleListener(new P2PLifecycleListener());
P2PLifecycleListener listener = new P2PLifecycleListener();
listener.setConfigLocation(configLocation);
tomcat.getServer().addLifecycleListener(listener);
} else {
tomcat.getServer().addLifecycleListener(new ClientServerLifecycleListener());
}
Expand Down Expand Up @@ -73,7 +75,9 @@ public void start() throws Exception {

@Override
public void stop() throws Exception {
tomcat.stop();
if (tomcat.getServer().getState().isAvailable()) {
tomcat.stop();
}
}

@Override
Expand Down

0 comments on commit 57371af

Please sign in to comment.