First, move Connection as an instance variable so that it can be retrieved from the open, close, and getMessage methods.
Then create an openConnection method with PostConstruct annotation.
@PostConstruct public void openConnection() { connection = connectionFactory.createConnection(); }
Finally, create a closeConnection method with a PreDestroy annotation.
@PreDestroy public void closeConnection() { connection.close(); }
source share