MongoDB Java Driver notes

A few notes on using the MongoDB Java Driver API:

Getting a connection:

MongoClient client = new MongoClient("localhost", 27017);
DB db = client.getDB("test");

Get a collection from connected db:

DBCollection collection = db.getCollection("example");

 

Find all docs in collection and iterate through results:

DBCursor c = collection.find();
try {
   while(c.hasNext()) {
       System.out.println(c.next());
   }
} finally {
   c.close();
}

 

Simple findOne query, matching a doc with properyname = value:

DBObject result = collection.findOne(new BasicDBObject("propertyname", "value"));

 

Find all, sort on property ‘example’ ascending (1=asc, -1=desc), and limit to 10 results:

DBCursor c = collection.find()
    .sort(new BasicDBObject("example", 1))
    .limit(10);

 

Serialize results of a cursor to JSON (tip from here):

JSON json = new JSON();
String jsonString = json.serialize(c);

Java Posse Podcast – now officially ended!

If you spotted this post on the Java Posse’s Google Group, then you might have already seen this link to a photo on Twitter from Devoxx 2014. If you were at Devoxx 2014 and attended the live Java Posse session, then you already know this too. Of course, if you regularly listen to the podcasts then you would have noticed that the regular podcast sessions dropped off noticeably last year.

The Java Posse’s final podcast session (#461), recorded from Devoxx 2014 is now live on their feed.

I’ve listened to the majority of their podcasts (and the early Javacast episodes) since the first sessions in 2005, it’s definitely the Java related technology podcast that I’ve listened to and followed the most, and so it’s sad that the guys have finally decided to call it quits and not continue the series any more.

Thanks to the guys for producing the session all these years, you’ll be missed!

So where are they now?

What am I listening too now? I’ve listened to a couple of episodes of the Java Pub House, which is ok, and on my todo list is Java Enterprise Newscast. But I don’t think you can replace The Java Posse. Thanks again guys, and good luck in your other endeavors.

Updating the endpoint URL for a generated JAX-WS client

When you generate a JAX-WS client from a locally deployed service, the URL for the endpoint and the WSDL are hardcoded into the generated client code. If you redeploy the service elsewhere (i.e. moving from a local development environment to a test or production environment), then you could regenerate against the new URL for the service, but the JAX-WS api does allow you to programmatically specify the endpoint and wsdl locations.

From this post, the key is to use the BindingProvider to specify a new value for BindingProvider.ENDPOINT_ADDRESS_PROPERTY:

BindingProvider bp = (BindingProvider)port;
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://your_url/ws/sample");

By itself though, this gave me an additional exception as it appears the client it still attempting to locate the WSDL. To work around this, you can also pass the new WSDL url location when you instantiate the generated client (this is discussed here). Before using the BindingProvider snippet above, pass the updated urls into the client like this:

EndpointService service = new EndpointService(
    new URL("http://your-url-to-endpoint/Endpoint?wsdl"),
    new QName("http://namespace-of-endpoint-from-wsdl/",
						"EndpointService"));

SpotCollectorEndpoint endpoint = service.getSpotCollectorEndpointPort();

 

Updating log4j 1.x to 2.x

I’ve used Log4J 1.x for ages, and not even realized that the 1.x code line is not maintained any more, it seems all the activity is on 2.x as the latest maintained version of the framework.

To move from 1.x to 2.x, there’s a few changes:

If you’re using Maven for your dependencies, replace

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
</dependency>

with

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0.2</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0.2</version>
</dependency>

The API has changed from:

org.apache.log4j.Logger

and

Logger.getLogger()

 

to

org.apache.logging.log4j.Logger

and

org.apache.logging.log4j.LogManager.getLogger()

 

Sample xml config – use filename log4j2.xml instead of log4j.xml (or log4j.properties):

<?xml version=”1.0″ encoding=”UTF-8″?>
<Configuration>
<Appenders>
<Console name=”STDOUT” target=”SYSTEM_OUT”>
<PatternLayout pattern=”%C – %m%n”/>
</Console>
</Appenders>
<Loggers>
<Logger name=”example.logger.name” level=”debug”/>
<Root level=”debug”>
<AppenderRef ref=”STDOUT”/>
</Root>
</Loggers>
</Configuration>

Additional useful info here.