Skip to content

Instantly share code, notes, and snippets.

View rponte's full-sized avatar
🏠
Working from home

Rafael Ponte rponte

🏠
Working from home
View GitHub Profile
@alexandreaquiles
alexandreaquiles / DiasUteis.java
Last active October 11, 2021 00:31
Passado um ano ou um mês ou alguns meses de um ano, gera um mapa (Map) dos meses (YearMonth) com a lista de datas (LocalDate) que são dias úteis (segunda a sexta).
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.YearMonth;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@alexandreaquiles
alexandreaquiles / DatasImportantesNaoTestavel.java
Last active January 23, 2017 20:06
Exemplo de como a classe Clock da nova API de datas do Java 8 permite uma maior testabilidade. Faz parte do post: http://blog.caelum.com.br/conheca-a-nova-api-de-datas-do-java-8/
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class DatasImportantesNaoTestavel {
public LocalDate diaDoProgramadorDesseAno(){
//como fazer para testar uma data do passado, por exemplo, de 2012?
return LocalDate.now().with(TemporalAdjusters.firstDayOfYear()).plusDays(255);
}
}
@ymartin59
ymartin59 / IterableListScrollableResults.java
Created April 1, 2014 13:42
IterableListScrollableResults<E> is a partial wrapper view as a List<E> on an Hibernate ScrollableResults. When an existing code process a large amount of Hibernate entities as a List<E>, it allows to work with data in stream without a large code refactoring.
import java.util.AbstractList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
/**
* A List<E> partial wrapper view on an Hibernate ScrollableResults.
@regispires
regispires / ContatoController2.java
Last active October 1, 2018 16:57
Controller JSF (ManagedBean) + Spring com SpringBeanAutowiringSupport
package br.ufc.quixada.npi.web;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.inject.Inject;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import br.ufc.quixada.npi.model.Contato;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import java.io.IOException;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
@WebFilter (value="/*",
initParams=({
@WebInitParam(name="duration", value="1")
@jyeary
jyeary / jsf.ajax.handler.js
Last active November 9, 2018 20:40
JSF AJAX client side handling code.
/*
* Copyright 2012-2014 John Yeary <[email protected]>.
*
* 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
@garcia-jj
garcia-jj / Page.java
Last active January 3, 2016 12:49
Pagination is really cool to avoid fuking the server performance :)
import static com.google.common.base.Objects.firstNonNull;
import java.io.Serializable;
/**
* Classe para prover paginação de resultados.
*
* @author Otávio Scherer Garcia
*/
public final class Page
@chanks
chanks / gist:7585810
Last active July 22, 2025 01:00
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

public class Validate {
public static boolean not(boolean expression){
return !expression;
}
public static boolean in(String value, String[] options){
return StringUtil.in(value, options);
}
@sergiolopes
sergiolopes / build.sh
Created October 28, 2013 13:58
Compressão de HTML, JSP e Tagfiles em build time usando htmlcompressor. O arquivo regex.txt exclui patterns (no caso, scriptlets e diretivas JSP).
java -jar war/WEB-INF/lib/htmlcompressor-*.jar \
--type html --recursive --mask '*.jsp;*.html;*.tag;*.htm' \
--compress-css --compress-js --js-compressor closure \
--remove-intertag-spaces --remove-quotes \
--preserve-server-script -p regex.txt \
-o war/ war/ 2> /dev/null