Prepending text to a file with sed

Appending content to a file on Linux is pretty easy with >> but there’s no equivalent redirection to prepend content. Luckily, for every file manipulation problem on Linux there’s at least a dozen solutions.

Using sed with -i for inplace replacement of content in a file, you can match ^ for start of line, and then follow with the content you want to replace (in this case, insert).

MongoDB Java api: using a sequence collection with findAndModify()

MongoDB doesn’t have an equivalent of sequences typically used in relational databases, since documents are automatically given a unique ObjectId value for the “_id” property when inserted.

To return data from documents via a REST api, it may be more useful to use a sequential unique key. The MongoDB docs have an example of how you could use a Collection to hold a document per sequence that you need, and increment a value property each time you retrieve it with findAndModify().

There’s a number of questions on StackOverflow related to this approach, most seem related to the approach doc linked above (e.g. here, here, and articles elsewhere, like here).

To implement this approach using the Java api, using findAndModify() seems to be key, as you need to ensure you are querying and incrementing the sequence value in a document in a single, atomic step. After that point once you have the updated/next value from your document holding your sequence, the fact that you use that value in a subsequent atomic insert to another document seems to be safe (please leave me a comment if this assumption is not correct!), as every call to findAndModify() to increment the sequence value is atomic (making an assumption here based on my limited MongoDB knowledge, but I think this is correct!).

Here’s how I implemented the approach using the Java api:

[code]
public int getNextSequence() throws Exception {
DB db = MongoConnection.getMongoDB();

DBCollection sequences = db.getCollection("sequences");

// fields to return
DBObject fields = BasicDBObjectBuilder.start()
.append("_id", 1)
.append("value", 1).get();

DBObject result = sequences.findAndModify(
new BasicDBObject("_id", "addressId"), //query
fields, // what fields to return
null, // no sorting
false, //we don’t remove selected document
new BasicDBObject("$inc", new BasicDBObject("value", 1)), //increment value
true, //true = return modified document
true); //true = upsert, insert if no matching document

return (int)result.get("value");
}
[/code]

 

Adding Spring Boot Actuator Metrics to a Jersey App

Spring Boot Actuator adds a number of health monitoring endpoints to support monitoring of your Spring Boot based applications. To add to an existing Spring Boot app, you just add a dependency on spring-boot-starter-actuator as described here.

This works out of the box for a Spring MVC based Spring Boot app, but if you’re building JAX-RS endpoints using Jersey, by default the Jersey resources and Actuator resources both get mapped to the root ( / ), so you need to map either Jersey or Actuator to something other than /. You can easily re-map Actuator be adding this line to application.properties (change /system as needed):

server.servlet-path=/system

This is described here: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-actuator.html#howto-use-actuator-with-jersey