Tag: java

Local javac path issues on older OpenShift WildFly8.2 based project

I have an app that I created a few months back on OpenShit based on the WildFly 8.2 cartridge. Locally in Eclipse the project builds and compiles fine, but executing mvn directly or in Netbeans (which also builds using your mvn pom.xml), it fails with an error regarding a path to javac:

-------------------------------------------------------------
COMPILATION ERROR : 
-------------------------------------------------------------
Failure executing javac, but could not parse the error:
/bin/sh: ${env.OPENSHIFT_WILDFLY_DIR}usr/lib/jvm/jdk1.8.0_05/bin/javac: bad substitution
1 error

This is obviously setup for building specifically in the OpenShift environment with this property defining the path to javac:

<maven.compiler.executable>${env.OPENSHIFT_WILDFLY_DIR}usr/lib/jvm/jdk1.8.0_05/bin/javac</maven.compiler.executable>

There’s a few posts and discussions about this (e.g. here and I suspect this is related), but I’m guessing the version of the pom.xml I have is older and been changed recently. I created a new OpenShift WildFly based project to compare the created pom.xml, and these two properties are no longer in the pom.xml file:

<maven.compiler.executable>${env.OPENSHIFT_WILDFLY_DIR}usr/lib/jvm/jdk1.8.0_05/bin/javac</maven.compiler.executable>
<maven.compiler.fork>true</maven.compiler.fork>

Removing them fixes my local builds, and pushing the code to OpenShift still seems to build ok too.

(Page views: 108)

Happy 20th Birthday Java! What’s your Java story?

I started learning Java in 1996, from the book ‘Learn Java in 21 Days’, published by SAMS. For some reason I even still have the same book:wpid-20150521_081822.jpg

At the time I was working for Royal Sun Alliance and we were doing OS/2 client/server development using Application Manager, against a CICS/COBOL backend. One of the guys I was working with, Mike, said I should take a look at this new language called Java because it can run on any platform. At that time developing on OS/2 and seeing demand slowing for OS/2 AM developers, the promise of a development language not tied to a specific platform seemed an attractive offer, and a potential ticket to move away from OS/2.

I started working through the exercises in the book, building my first Applets – I think one of the first applets I built was a lottery ticket number generator that I had on my webpage at the time. In 1996 it was pretty cool to have interactive content on your web site! Even though this turned out to not be Java’s strong point (most Java development is arguably server side now days), it was where it all got started.

Although I continued to play with Java in my own time, it wasn’t until I changed jobs in 1997 and moved to TSW/Indus, a software house developing Enterprise Asset Manager software, that I had the opportunity to start Java development ‘on the job’. At the time the company had a Powerbuilder/Oracle PL/SQL based client/server product, and while in that first year I cross trained to Powerbuilder, we started prototypes of building a web-based replacement for the Powerbuilder client, against a Java backend. At the time this was using a ‘Java Cartridge’ on Oracle Application Server (I think it was called), and the Java code was invoked CGI-BIN style using HTTP GET query string parameters that were passed into Java code on the server.

At some point pretty soon after these first prototypes the Servlet API came along, and then EJB1.x came along… looking back which was an absolute nightmare from a developer productivity point of view. Somewhere in there I also dabbled with some CORBA based backend running on a server that I think was called IONA (although those memories are long gone now).

I’ve seen a lot of incredible changes in the Java space, and 19 years later from where I started, I’m still coding Java and it’s still going strong!

(Page views: 136)

Enjoy your additional ‘leap second’ on June 30th 2015

This year on June 30th at 23:59:59 UTC, rather than the 59th second ticking to 00, there is an additional second added so clocks will tick to 60 before going to 00.

If this sounds rather odd, check out a more detailed explanation here on Wikipedia.

So what does this mean for us as software developers? Well, for Java JVMs, you can use the provided tzupdater tool (updates described here, and usage described here), download the updated timezone file and apply the update. For other platforms and operating systems, check release notes and updates from your vendors (e.g. For RHEL, check here).

Should you be worried about the impact to your systems? Well that depends on how sensitive your system is to the date/time and synchronization with other systems that you integrate with. For example, the ICE futures exchange have announced they are taking their systems down for an hour overlapping with 23:59:59 on June 30th to avoid any unexpected issues. Whether you have to go that far depends on your system.

 

(Page views: 169)

JEP 222 – REPL proposed for Java SE9

A REPL (Read-Eval-Print-Loop) feature is becoming a common feature for many languages (Scala and Groovy both have a REPL commandline shell), and JEP-222 proposes adding this same feature to Java for SE9. This feature is useful for quickly trying out and evaluating an expression, and will be a valuable addition to Java SE.

(Page views: 111)

Finding min and max values with MongoDB Aggregation

Aggregation operations in MongoDB allow you to process documents to produce computed results. An example is to search for a min or max value. For documents that look like this:

{
  "_id" : ObjectId("5521ae8c0364d302910ec5eb"),
  "color" : "blue",
  "orderDate" : ISODate("2014-07-08T19:53:00Z")
  //other properties
}

You can find the min/earliest date in the collection for an document with matching color of blue with an aggregation query like this:

db.collectionName.aggregate(
[
  { "$match" : { "color":"blue" } },
  { "$group" : {
    "_id" : "$color",
    first: { "$min" : "$orderDate"},
  }
])

Implementing this with the Java Driver API looks like this:

DBObject match = new BasicDBObject("$match", new BasicDBObject("color", "blue"));

 //$group
 DBObject groupFields = new BasicDBObject( "_id", "$color");
 groupFields.put("orderDate", new BasicDBObject( "$min", "$orderDate"));
 DBObject group = new BasicDBObject("$group", groupFields);

 List<DBObject> pipeline = Arrays.asList(match, group);

 AggregationOutput output = col.aggregate(pipeline);
 for (DBObject result : output.results()) {
   //iterate results, only 1 for $min
 }

(Page views: 123)

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);

(Page views: 94)

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.

(Page views: 290)

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();

 

(Page views: 291)

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.

(Page views: 400)

Adding a JMS Queue to Wildfly 8.x

The messaging subsystem in Wildfly is enabled in the standalone-full.xml config (not standalone.xml).

To add a new queue, search for <subsystem xmlns=”urn:jboss:domain:messaging:2.0″>, and then within the <hornetq-server> section, add a new <jms-destinations> if it doesn’t exist already, and define your queue name and JNDI lookup:

<jms-destinations>
    <jms-queue name="exampleQueue">
        <entry name="jms/queue/example"/>
        <entry name="java:jboss/exported/jms/queue/example"/>
    </jms-queue>
</jms-destinations>

 

(Page views: 663)