Skip to content

Instantly share code, notes, and snippets.

@mojavelinux
mojavelinux / ApplicationInitializer.java
Created October 21, 2010 04:53
A bridge between the ServletContext life cycle events and CDI observers (with example observer ApplicationInitializer)
// uncomment line if you want the instance to be retained in application scope
// @ApplicationScoped
public class ApplicationInitializer
{
public void onStartup(@Observes @Initialized ServletContext ctx)
{
System.out.println("Initialized web application at context path " + ctx.getContextPath());
}
}
@lindenb
lindenb / Delicious2Diigo.java
Created December 19, 2010 14:48
Save bookmarks from delicious to Diigo.com using the Diigo API.
/**
* Delicious2Diigo
*
* Author:
* Pierre Lindenbaum
* http://plindenbaum.blogspot.com
*
* Motivation:
* save bookmarks from delicious to diigo using the Diigo API.
*
@davide-romanini
davide-romanini / Repositories.java
Created April 15, 2011 08:15
CDI producer for spring-data-jpa based repositories
public interface PersonRepository extends JpaRepository<Person, Long>, JpaSpecificationExecutor<Person> {
List<Person> findByEmailAddressAndLastname(String emailAddress, String lastname);
// find all persons and apply eclipselink batch hint to fetch their addresses
@Query("select p from Person p")
@QueryHints({@QueryHint(name="eclipselink.batch", value="p.addresses")})
List<Person> findAllWithAddresses();
}
public class Repositories {
@palesz
palesz / BasicAuthAuthorizationInterceptor.java
Created August 23, 2012 16:13
Basic HTTP Authentication Interceptor for Apache CXF
import org.apache.cxf.binding.soap.interceptor.SoapHeaderInterceptor;
import org.apache.cxf.configuration.security.AuthorizationPolicy;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;
import org.apache.cxf.transport.Conduit;
import org.apache.cxf.ws.addressing.EndpointReferenceType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
anonymous
anonymous / EnumerationIndoUtil
Created July 12, 2013 10:25
public class EnumerationIndoUtil {
public static <T1> Enumeration<List<T1>> createPartitionedEnumeration(final List<T1> originalList, final int blockSize) {
class PartitionedEnum implements Enumeration<List<T1>> {
int lastIndex = 0;
public boolean hasMoreElements() {
@chanks
chanks / gist:7585810
Last active June 22, 2024 19:01
Turning PostgreSQL into a queue serving 10,000 jobs per second

Turning PostgreSQL into a queue serving 10,000 jobs per second

RDBMS-based job queues have been criticized recently for being unable to handle heavy loads. And they deserve it, to some extent, because the queries used to safely lock a job have been pretty hairy. SELECT FOR UPDATE followed by an UPDATE works fine at first, but then you add more workers, and each is trying to SELECT FOR UPDATE the same row (and maybe throwing NOWAIT in there, then catching the errors and retrying), and things slow down.

On top of that, they have to actually update the row to mark it as locked, so the rest of your workers are sitting there waiting while one of them propagates its lock to disk (and the disks of however many servers you're replicating to). QueueClassic got some mileage out of the novel idea of randomly picking a row near the front of the queue to lock, but I can't still seem to get more than an an extra few hundred jobs per second out of it under heavy load.

So, many developers have started going straight t

@mulderbaba
mulderbaba / ReloadableResourceBundleMessageSource.java
Created December 15, 2013 11:45
ReloadableResourceBundleMessageSource class that is used in RebelLabs blog post that explains message bundle mechanism.
package com.zeroturnaround.rebellabs;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.support.AbstractMessageSource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.Assert;
import org.springframework.util.DefaultPropertiesPersister;
@harith
harith / shellmarks.sh
Last active September 18, 2022 07:50
Utilities to let you easily reach frequently visited but deeply nested directories.
# Utilities for quickly accessing frequently used directories in bash.
# Usage:
# $ cd /path/to/project/src/
# $ mark code # Will create a new shortcut.
# # Becomes interactive if a shortcut already exists
# # m is an alias for mark. You can also `m code`
#
# $ code # From now on, running this anywhere in the shell
# # will put you in /path/to/project/src/code
@romankierzkowski
romankierzkowski / 0_falsy_.js
Last active February 22, 2020 11:25
True, False and Equal in JS
/* false, 0, undefined, null, NaN, "" are false */
> Boolean(false)
false
> Boolean(0)
false
> Boolean("")
false
> Boolean(undefined)
false
@kosta
kosta / inout.java
Created March 26, 2014 06:47
Copy stdin to stdout in Java
import java.io.IOException;
/**
* Class that copies stdin to stdout, as compained about as not being cleanly
* writable in Java on Hacker News.
* In real code, you would just write IOUtils.copy(System.in, System.out),
* which does basically the same thing.
* This does not catch any exceptions as a) this is just an "exercise" and
* b) all we could do with them is pretty-print them. So let the runtime
* print them for you.