Skip to content

Instantly share code, notes, and snippets.

@vmarcinko
vmarcinko / build.gradle
Last active December 18, 2015 11:39
Example files for my blog post about Hadoop job programmatic submission
apply plugin: 'java'
archivesBaseName = 'bigdatalab'
group = 'bigdatalab'
version = '1.0'
ext {
slf4jVersion = '1.7.5'
hadoopVersion = '1.0.4'
cascadingVersion = '2.1.+'
@vmarcinko
vmarcinko / JobHelper
Created June 17, 2013 19:56
Helper utilities for Hadoop job submission
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.mapreduce.JobSubmissionFiles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
@vmarcinko
vmarcinko / EmbeddedKafkaCluster.java
Created August 16, 2014 13:34
Embedded Zookeeper & Kafka cluster
import kafka.server.KafkaConfig;
import kafka.server.KafkaServer;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
@vmarcinko
vmarcinko / CategoryEnumInfo.java
Created January 21, 2015 16:03
Nanocube DMP encoder in java
public class CategoryEnumInfo {
private final String label;
private final Byte encodedValue;
public CategoryEnumInfo(String label, Byte encodedValue) {
this.label = label;
this.encodedValue = encodedValue;
}
public String getLabel() {
@vmarcinko
vmarcinko / UndertowServer.java
Created April 28, 2015 06:21
Undertow-Spring MVC integration
package vmarcinko.undertow;
import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.PathHandler;
import io.undertow.server.handlers.RedirectHandler;
import io.undertow.server.handlers.resource.FileResourceManager;
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
@Embeddable
public class QueueingState {
public enum Status {
NOT_ATTEMPTED,
ERROR,
SUCCESS
}
private Status status;
public interface QueueConsumerModule<ID> {
List<ID> findItemIdsWhereQueueingNextAttemptTimeIsBefore(LocalDateTime time, int limit);
Optional<QueueingState> getQueueingStateForItem(ID itemId);
Optional<QueueingState> processItem(ID itemId);
}
private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
// ...
private void startProcessingTask() {
logger.info("Starting queue processing task with delay of {} secs", this.pollingPeriodInSecs);
Runnable command = this::processQueuedItems;
this.processingTask = this.scheduledExecutorService.scheduleWithFixedDelay(command, pollingPeriodInSecs, pollingPeriodInSecs, TimeUnit.SECONDS);
}
public void processQueuedItems() {
try {
LocalDateTime now = LocalDateTime.now();
List<?> itemIds = this.queueConsumerModule.findItemIdsWhereQueueingNextAttemptTimeIsBefore(now, itemsPollSize);
if (!itemIds.isEmpty()) {
logger.info("Fetched {} pending queued items", itemIds.size());
for (Object itemId : itemIds) {
processItemAndHandleErrorIfRaised(itemId);
}
@Entity
@Table(name = "sms_message", indexes = @Index(name = "idx_sms_msg_queue_polling_fields", columnList = "next_attempt_time"))
public class SmsMessage {
@Id
@Column(name = "id", nullable = false)
@GeneratedValue
private Long id;
@Column(name = "uid", nullable = false, unique = true)
private String uid; // app-assigned unique ID