Skip to content

Instantly share code, notes, and snippets.

View sjednac's full-sized avatar

Szymon Jednac sjednac

View GitHub Profile
@sjednac
sjednac / BusyApp.java
Last active May 11, 2021 07:03
JVM monitoring in a Docker environment
public final class BusyApp {
public static void main(String []args) throws Exception {
final java.util.Random generator = new java.util.Random();
while (true) {
generator.ints(1000000, 0, 100).sorted();
Thread.sleep(5000L);
}
}
}
@sjednac
sjednac / TimeSeries.scala
Last active June 4, 2018 14:32
Computing local information within a rolling time window in Scala
import scala.io.Source
case class Observation(time: Int, value: BigDecimal)
class Memory(val length: Int, val observation: Option[Observation], val pastEvents: List[Observation]) {
def record(newObservation: Observation): Memory = {
val newEvents = (newObservation :: pastEvents).takeWhile(_.time > newObservation.time - length)
new Memory(length, Some(newObservation), newEvents)
}
@sjednac
sjednac / ZooKeeperReader.scala
Last active September 25, 2015 19:58
A minimal property reader for Apache ZooKeeper
import java.time._
import org.apache.curator.framework._
import org.apache.curator.retry._
import org.apache.zookeeper.CreateMode._
object ZooKeeperReader extends App {
val connectionString = "192.168.59.201:2181,192.168.59.202:2181,192.168.59.203:2181,192.168.59.204:2181,192.168.59.205:2181"
val retryPolicy = new RetryOneTime(1000)
val client = CuratorFrameworkFactory.newClient(connectionString, retryPolicy)
@sjednac
sjednac / SpringAndSeleniumScalaTest.scala
Created May 28, 2015 22:32
A Selenium test run with ScalaTest on a Spring Boot application.
import org.junit.runner.RunWith
import org.scalatest.selenium.HtmlUnit
import org.scalatest.{FlatSpec, ShouldMatchers}
import org.springframework.boot.test.{SpringApplicationConfiguration, WebIntegrationTest}
import org.springframework.test.context.TestContextManager
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
@RunWith(classOf[SpringJUnit4ClassRunner])
@SpringApplicationConfiguration(classes = Array(classOf[Application]))
@WebIntegrationTest(value = Array("server.port=9000"))
@sjednac
sjednac / UnboundedKnapsackDP.java
Last active June 4, 2017 06:01
Unbounded knapsack solver using dynamic programming approach.
import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
@sjednac
sjednac / OptionalSpringBeanInjection.scala
Created December 29, 2014 16:57
Optional Spring bean injection using a @Profile
import collection.JavaConversions._
import org.springframework.beans.factory.annotation._
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation._
import org.springframework.stereotype._
package com.example.core {
@ComponentScan(basePackages = Array("com.example.core"))
@Configuration
@sjednac
sjednac / SimpleScalatraRestService.scala
Created December 8, 2014 18:31
A simple Scalatra REST service launched with Jetty
import javax.servlet.ServletContext
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.webapp.WebAppContext
import org.json4s.{DefaultFormats, Formats}
import org.scalatra._
import org.scalatra.json._
import org.scalatra.servlet.ScalatraListener
case class User(id: Int, name: String)
@sjednac
sjednac / Log4j2WithMongoAppenderDemo.scala
Last active August 29, 2015 14:09
Log4j 2.x with MongoDB & SLF4J
object Log4j2WithMongoAppenderDemo extends App {
val logger = org.slf4j.LoggerFactory.getLogger(Log4j2WithMongoAppenderDemo.getClass)
logger.trace("A trace message")
logger.debug("A debug message")
logger.info("An info message")
logger.warn("A warning message")
logger.error("An error message")
logger.error("An error message with an exception", new Exception("Some serious problem"))
@sjednac
sjednac / RaptureIORedditReader.scala
Last active August 29, 2015 14:07
An example Rapture IO client for a RESTful web service.
import rapture._
import core._, io._, net._, uri._, json._, codec._
import encodings.`UTF-8`
import jsonBackends.jackson._
case class PostContainer(kind: String, data: Post)
case class Post(id: String, subreddit: String, score: Int, title: String, author: String, permalink: String) {
lazy val topComment = {
val json = Json.parse(uri"http://www.reddit.com/r/${subreddit}/comments/${id}.json?sort=top&limit=1&depth=1".slurp[Char])
@sjednac
sjednac / checkstyle-pre-commit.py
Last active June 23, 2016 09:30
Checkstyle pre-commit hook
#! /usr/bin/env python
# -*- encoding: utf-8 -*-
import os
# Config
checkstyle_jar = os.getenv('CHECKSTYLE_JAR', "checkstyle-5.7-all.jar")
checkstyle_cfg = os.getenv('CHECKSTYLE_CFG', "checkstyle.xml")
failing_test_should_prevent_commit = True