Simple transacted JMS consumer

Ever looked for a simple JMS Consumer template – here is one and it is transacted too 🙂

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.naming.InitialContext;


public class Consumer {

    public static void main(String[] args) throws Exception {

        InitialContext context = new InitialContext();
        ConnectionFactory factory = 
                       (ConnectionFactory) context.lookup("ConnectionFactory");

        Connection connection = factory.createConnection();
        connection.start();

        // Start the transaction
        Session session = 
                    connection.createSession(true, Session.SESSION_TRANSACTED);

        Destination destination = (Destination) context.lookup("test");
        MessageConsumer consumer = session.createConsumer(destination);

        Message message;

        try {
            // Get all messages of the queue
            while ((message = consumer.receive(3000)) != null) {
                System.out.println("Message: " + message);
            }
            // All is well - commit transaction
            session.commit();

        } catch (Exception e) {
            e.printStackTrace();
            // Something is wrong - rollback transaction
            session.rollback();
        }

        // Cleanup
        session.close();
        connection.close();
        context.close();

    }
}

This solution depends on JNDI so here is the jndi.properties also:

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory

# Use the following property to configure the default connector
java.naming.provider.url = tcp://localhost:61616

# Register some queues in JNDI using the form
# queue.[jndiName] = [physicalName]
queue.test = NIKLAS.TEST

I’m using an ActiveMQ server as JMS provider which works good with the ActiveMQ client:
pom.xml

 
       
            org.apache.activemq
            activemq-client
            5.7.0
        

Tested on Windows 10, Maven 3.8.4, Java 11.0.18 and ActiveMQ Client 5.7.0

Apt Cheat Sheet

Update the local package library

apt-get update

Upgrade all packages

apt-get upgrade

Upgrade ONE package

apt-get install --only-upgrade git

Install a package

apt-get install <package name>

Install a specific version

apt install <package name>=<version> # ex. apt install git=1.25.1

Remove a package

apt-get remove <package name>

Remove unused dependencies

apt-get autoremove

Search for a package

apt-cache search <package name>

Show package info

apt show <package name>

Lista all versions for a package

apt list --all-versions <package name>

My Docker Cheat Sheet

Run a new container

docker run <container id or name>

Start a stopped container

docker start <container id or name>

Stop a container

docker stop <container id or name>

Kill a container

docker kill <container id or name>

Restart a container

docker restart <container id or name>

List all running containers

docker ps

List all containers

docker ps -a

List with formatted output (here only show name and status of containers)

docker ps --format '{{.Names}} {{.Status}}'

Inspect a container

docker inspect <container id or name>

List all ports a container uses

docker container port <container id or name>

Open Bash in container (‘exit’ to close)

docker exec -it <container id or name> bash

Watch STDOUT from container (‘-f’ for continued logging)

docker logs <container id or name>

Search containers in registry (example: “MySQL” gives all available MYSQL-containers)

docker search <search word>

Pull container from registry

docker pull <name>