JAX-RS Server API Snippets

Because a lot of my current projects are using JAX-RS in different versions I’d like to write down and share some frequently used snippets for implementing RESTful web-services with the JAX-RS specification here. Using RegEx in Path Expressions Sometimes we need to extract multiple parameters from a path expression e.g. in the following example where year, month and day are fragments if the path. @GET @Path("/orders/{year:\\d{4}}-{month:\\d{2}}-{day:\\d{2}}") @Produces(MediaType.TEXT_PLAIN) public Response getOrders(@PathParam("year") final int year, @PathParam("month") final int month, @PathParam("day") final int day) { return Response.ok("Year: " + year + ", month: " + month + ", day: " + day).build(); } ...

September 28, 2014 · 5 min · 924 words · Micha Kops

Java EE 7 JMX Reports with Yammer Metrics

There are several ways to aggregate and report application performance indicators in a Java application. One common way here is to use Java Management Extensions (JMX) and MBeans. The Yammer Metrics Library eases this task for us and simplifies the aggregation of different reports. In the following tutorial, we’re going to set up a full Java EE 7 web application by the help of Maven archetypes and we’re running the application on WildFly application server that is downloaded and configured completely by the WildFly Maven Plugin. ...

August 26, 2014 · 10 min · 2117 words · Micha Kops

Java EE 7 Database Migrations with Liquibase and WildFly

I have written about other database migration frameworks before but in this article I’d like to cover the Liquibase framework in combination with WildFly as Java EE 7 compatible application server. In the following tutorial, we’re going to write a full Java EE 7 book store application with a few steps and with Liquibase on board to create the database structure and insert example data into the database. Thanks to the WildFly Maven Plug-in we even do not need to download and configure the application server but let Maven and the plug-in do the work for us. ...

July 31, 2014 · 9 min · 1771 words · Micha Kops

Writing Java Integration Tests for MongoDB

MongoDB is matured, document-oriented, cross-platform NoSQL database system with drivers available for a bunch of different programming languages. In the following short examples I’m going to write some integration tests for MongoDB using the MongoDB Java driver and the Flapdoodle library to create an embedded MongoDB instance for testing. We’re going to write tests for a simple persist-and-query scenarion and for a map-reduce function and in addition I’m going to show how to bind the start and stop of a MongoDB instance to a Maven goal using the embedmongo-maven-plugin. ...

October 16, 2013 · 8 min · 1585 words · Micha Kops

Creating a Chat Application using Java EE 7, Websockets and GlassFish 4

Java EE 7 is out now and so I was curious to play around with the new specifications and APIs from in this technology stack. That’s why I didn’t hesitate to add yet another websocket-chat tutorial to the existing ones on the internet in favour of gathering some experience with this technology and a possible integration using a GlassFish 4 server, the new Java API for JSON Processing for data serialization combined with custom websocket encoders/decoders and finally adding some Bootstrap and jQuery on the client side. ...

August 13, 2013 · 8 min · 1591 words · Micha Kops

Java Snippets

Remote Debug a Pod’s Java Process Simple steps for remote debugging a Java process running on a k8 pod: Edit deployment and add the following parameters to the Java start line: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=127.0.0.1:5005 Also add the following port mapping at the section container → ports in the deployment: - containerPort: 5005 protocol: TCP Safe, wait for the new pods and then add a port forward for port 5005 for this pod: kubectl port-forward podname 5005 ...

March 1, 2010 · 13 min · 2583 words · Micha Kops

jq Snippets

Sample JSON File Sample JSON File containing well known programmers that we use for the following examples coders.json [ { "name": "Bjarne Stroustrup", "languages": [ { "name": "C++", "year_created": 1983 } ], "details": { "nationality": "Danish", "awards": ["IEEE Computer Society Computer Pioneer Award", "Charles Stark Draper Prize"] } }, { "name": "Guido van Rossum", "languages": [ { "name": "Python", "year_created": 1991 } ], "details": { "nationality": "Dutch", "awards": ["Free Software Foundation Award for the Advancement of Free Software", "NLUUG Award"] } }, { "name": "James Gosling", "languages": [ { "name": "Java", "year_created": 1995 } ], "details": { "nationality": "Canadian", "awards": ["Order of Canada", "The Economist Innovation Award"] } }, { "name": "Dennis Ritchie", "languages": [ { "name": "C", "year_created": 1972 }, { "name": "Unix", "year_created": 1969 } ], "details": { "nationality": "American", "awards": ["Turing Award", "National Medal of Technology"] } }, { "name": "Brendan Eich", "languages": [ { "name": "JavaScript", "year_created": 1995 } ], "details": { "nationality": "American", "awards": ["Webby Award"] } } ] ...

March 1, 2010 · 2 min · 223 words · Micha Kops

Linux Snippets

These are not only linux snippets but also bash snippets and snippets using tools that run under Linux, *nix or sometimes even MacOSX, I should reorder this article someday ;) Settings for more reliable bash scripts set -euo pipefail this gives us …​ -e: exit script if a single command fails -u: exit script if an unset variable is used -o pipefail: return value of a pipeline is the status of the last command to exit with a non-zero status, or zero if no command exited with a non-zero status ...

March 1, 2010 · 15 min · 3006 words · Micha Kops

Postgres Snippets

Get size of a table SELECT pg_size_pretty(pg_total_relation_size('schemaname.tablename')); Select unique combination of fields / tuples SELECT DISTINCT ON(field1, field2) field1, field2 FROM thetable Select rows where a combination of fields is not unique SELECT columnA, columnB, count(*) AS count FROM thetable GROUP BY columnA, columnB HAVING count(*) > 1 Search for rows with array containing value Assuming, the field appointments has the type date[] SELECT * FROM mtable WHERE appointments @> ARRAY['2023-09-19'::date] ...

March 1, 2010 · 8 min · 1655 words · Micha Kops