Skip to content

Instantly share code, notes, and snippets.

View enesakar's full-sized avatar

Enes Akar enesakar

View GitHub Profile
@enesakar
enesakar / gist:1991989
Created March 7, 2012 08:39
hazel dep
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
@enesakar
enesakar / gist:1999418
Created March 8, 2012 07:35
Hazelcast MapStore Implementation
public class MyMapStore implements MapStore<String, Contact> {
private AmazonSimpleDB sdb;
private void init() {
try {
sdb = new AmazonSimpleDBClient(new PropertiesCredentials(getClass().getClassLoader().getResourceAsStream("AwsCredentials.properties")));
createDomainIfNotExists("Contact");
} catch (IOException e) {
e.printStackTrace();
@enesakar
enesakar / gist:1999425
Created March 8, 2012 07:37
hazelcast simpledb load implementation
public Contact load(String key) {
SelectRequest req = new SelectRequest("select * from Contact where itemName() = '" + key + "'");
SelectResult result = sdb.select(req);
if (!result.getItems().isEmpty()) {
Item item = result.getItems().get(0);
return formContact(item.getAttributes());
} else {
return null;
}
}
@enesakar
enesakar / gist:1999428
Created March 8, 2012 07:38
Hazelcast mapstore config
<map-store enabled="true">
<class-name>com.hazelsimpledb.service.MyMapStore</class-name>
<write-delay-seconds>0</write-delay-seconds>
</map-store>
@enesakar
enesakar / gist:1999536
Created March 8, 2012 07:58
hazel deps
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>1.9.4.4</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-cloud</artifactId>
<version>1.9.4.4</version>
</dependency>
package com.hazelsimpledb.model;
import java.io.Serializable;
public class Contact implements Serializable{
private String name;
private String phone;
....
}
public class MyMapStore implements MapStore<String,Contact> {
public void store(String key, Contact contact) {
DataService.getInstance().addContact(contact.getPhone(), contact.getName());
}
public void storeAll(Map<String, Contact> contactMap) {
for(String key: contactMap.keySet()) {
store(key, contactMap.get(key));
}
}
public void addContact(String phone, String name) {
List<ReplaceableAttribute> attrList = new ArrayList<ReplaceableAttribute>();
attrList.add(new ReplaceableAttribute("Phone", phone, true));
attrList.add(new ReplaceableAttribute("Name", name, true));
sdb.putAttributes(new PutAttributesRequest("Contact", name, attrList));
}
public Contact loadContact(String name) {
SelectRequest req = new SelectRequest("select * from Contact where itemName() = '" + name + "'");
SelectResult result = sdb.select(req);
if (!result.getItems().isEmpty()) {
<map-store enabled="true">
<class-name>com.hazelsimpledb.service.MyMapStore</class-name>
<write-delay-seconds>0</write-delay-seconds>
</map-store>
private void save(HttpServletRequest request) {
String phone = request.getParameter("phone");
String name = request.getParameter("name");
Map<String, Contact> mapContacts = Hazelcast.getMap("Contacts");
mapContacts.put(name, new Contact(name, phone));
request.setAttribute("message","Contact has been added.");
}
private void search(HttpServletRequest request) {
Map<String, Contact> mapContacts = Hazelcast.getMap("Contacts");