Skip to content

Instantly share code, notes, and snippets.

View mpilone's full-sized avatar

Mike Pilone mpilone

View GitHub Profile
public boolean tryAcquire(int timeout) {
SemaphorePermit permit = new SemaphorePermit(semaphoreName, groupName,
acquirerName, null);
// Delegate to the gatekeeper which enforces the permit rules based
// on the semaphore definition. The gatekeeper might be a local library
// or a call to a remote service.
permit = gatekeeperGateway.tryAcquire(permit);
Date expirationDate = permit.getExpirationDate();
final static String KEY_PREFIX = "gatekeeper";
ConcurrentMap definitionMap = hazelcast.getMap(KEY_PREFIX + ".semaphoreDefinitions");
ConcurrentMap permitMap = hazelcast.getMap(KEY_PREFIX + ".semaphorePermits");
// Logic:
// - Lookup the definition
// - Check the group and acquirer pattern
// - Check for an available permit
// - Issue the permit
final static String KEY_PREFIX = "gatekeeper";
ConcurrentMap definitionMap = hazelcast.getMap(KEY_PREFIX + ".semaphoreDefinitions");
ConcurrentMap permitMap = hazelcast.getMap(KEY_PREFIX + ".semaphorePermits");
Lock gatekeeperLock = hazelcast.getLock(KEY_PREFIX + ".lock");
if (gatekeeperLock.tryLock(LOCK_WAIT_PERIOD, TimeUnit.SECONDS)) {
try {
// Logic:
@mpilone
mpilone / HazelcastMQStompExample.java
Last active August 29, 2015 14:02
An example of creating a reliable, distributed, and elastic STOMP server using HazelcastMQ on top of Hazelcast.
HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
HazelcastMQConfig mqConfig = new HazelcastMQConfig();
mqConfig.setHazelcastInstance(hazelcast);
HazelcastMQInstance mqInstance = HazelcastMQ.newHazelcastMQInstance(mqConfig);
// Create the server.
HazelcastMQStompConfig stompConfig = new HazelcastMQStompConfig(mqInstance);
HazelcastMQStompInstance stompServer = HazelcastMQStomp.newHazelcastMQStompInstance(stompConfig);
@mpilone
mpilone / Throttle.rb
Last active January 3, 2016 02:09
MOVED: This plugin is now merged into Logstash core. -- A Throttle filter for LogStash. The filter can throttle the number of events received which is extremely useful when sending alerts via email.
require "logstash/filters/base"
require "logstash/namespace"
# The throttle filter is for throttling the number of events received. The filter
# is configured with a lower bound, the before_count, and upper bound, the after_count,
# and a period of time. All events passing through the filter will be counted based on
# a key_field. As long as the count is less than the before_count or greater than the
# after_count, the event will be "throttled" which means the filter will be considered
# successful and any tags or fields will be added.
#
@mpilone
mpilone / SpringAdLdapTest.java
Created November 21, 2013 14:38
A simple example of using Spring LDAP to authenticate a user against Active Directory.
// Setup the LDAP client (normally done via Spring context file).
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl("ldap://adserver.mycompany.com:3268");
contextSource.setBase("DC=AD,DC=MYCOMPANY,DC=COM");
contextSource.setUserDn("readonlyuser@ad.mycompany.com");
contextSource.setPassword("password1");
contextSource.afterPropertiesSet();
LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
ldapTemplate.afterPropertiesSet();
@mpilone
mpilone / BackgroundUiTask.java
Last active January 30, 2019 16:36
A background task that can be executed to perform background work and safe UI updates in Vaadin using the v7.1 UI.access() method. Currently you implement it by extension but it may make more sense to provide an interface for the doWork and doUiUpdate methods similar to the relationship between Thread and Runnable.
package org.mpilone.vaadin;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.UI;
import com.vaadin.ui.UIDetachedException;
/**
@mpilone
mpilone / spring-shiro-context.xml
Created July 31, 2013 15:27
Vaadin, Shiro, and Push. Support classes to integrate Shiro with Vaadin 7.1+ push support. See http://mikepilone.blogspot.com/ for more information.
<bean id="sessionManager"
class="org.mpilone.util.shiro.VaadinSessionManager" />
<bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager"
p:realm-ref="mpiloneRealm"
p:sessionManager-ref="sessionManager" />
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<bean id="mpiloneRealm" class="org.mpilone.util.shiro.MyCustomRealm" />
@mpilone
mpilone / VaadinSessionManager.java
Created July 31, 2013 15:26
Vaadin, Shiro, and Push. Support classes to integrate Shiro with Vaadin 7.1+ push support. See http://mikepilone.blogspot.com/ for more information.
package org.prss.mpilone.util.shiro;
import java.util.UUID;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionException;
import org.apache.shiro.session.mgt.*;
import com.vaadin.server.VaadinSession;
@mpilone
mpilone / VaadinSecurityContext.java
Created July 31, 2013 15:00
Vaadin, Shiro, and Push. Support classes to integrate Shiro with Vaadin 7.1+ push support. See http://mikepilone.blogspot.com/ for more information.
package org.mpilone.util.shiro;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.mpilone.util.shiro.SecurityContext;
import com.vaadin.server.VaadinSession;