If you are using the Spring Framework, the most straightforward way to do this is:
private Integer laancNotifyPort = SocketUtils.findAvailableTcpPort();
You can also set an acceptable range, and it will search in this range:
private Integer laancNotifyPort = SocketUtils.findAvailableTcpPort(9090, 10090);
This is a convenience method that abstracts away the complexity but internally is similar to a lot of the other answers on this thread.