Skip to content
Snippets Groups Projects
Commit b5e1929f authored by Joel Cavat's avatar Joel Cavat
Browse files

Simple example

parents
No related branches found
No related tags found
No related merge requests found
File added
pom.xml 0 → 100644
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.hepia.shop_online</groupId>
<artifactId>client-example</artifactId>
<packaging>jar</packaging>
<version>0.1</version>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>ch.hepia.shop_online</groupId>
<artifactId>messages</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- The plugin provides 2 goals to help execute system and Java programs. -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>ch.hepia.shop_online.Client</mainClass>
</configuration>
</plugin>
<!-- The Surefire Plugin is used during the test phase of the build lifecycle
to execute the unit tests of an application -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<groupId>ch.hepia.shop_online</groupId>
<artifactId>messages</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<file>${basedir}/dependencies/messages-0.1.jar</file>
<generatePom>true</generatePom>
</configuration>
<executions>
<execution>
<id>install-jar-lib</id>
<goals>
<goal>install-file</goal>
</goals>
<phase>validate</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package ch.hepia.shop_online;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;
public class Client {
public static void main(String[] args) throws JMSException {
TransactionApi api = TransactionApi
.withLocation(args.length == 1 ? args[0] : "localhost:61616")
.withMessage( (Message message) -> {
try {
if (message instanceof ObjectMessage objMessage && objMessage.getObject() instanceof Event.TransactionStatus t) {
if (t instanceof Event.TransactionAccepted ta) {
System.out.println("Transaction accepted: " + ta.transactionId());
} else if (t instanceof Event.TransactionRefused ta) {
System.out.println("Transaction refused: " + ta.transactionId() + " " + ta.reason());
}
}
} catch (JMSException e) {
e.printStackTrace();
}
}
);
api.send( new Command.CreateTransaction(java.util.UUID.randomUUID().toString(), "jojo", 10_10) );
api.send( new Command.CreateTransaction(java.util.UUID.randomUUID().toString(), "jojo", 100_50) );
api.send( new Command.CreateTransaction(java.util.UUID.randomUUID().toString(), "jojo", 1000_00) );
}
}
package ch.hepia.shop_online;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class TransactionApi {
private static int ackMode = Session.AUTO_ACKNOWLEDGE;
private static String clientQueueName = "client.messages";
private boolean transacted = false;
private Session session;
private MessageProducer producer;
static class TransactionBuilder {
private String ipBroker;
public TransactionBuilder(final String ipBroker) {
this.ipBroker = ipBroker;
}
public TransactionApi withMessage(MessageListener messageListener) {
return new TransactionApi(this.ipBroker, messageListener);
}
}
private TransactionApi(final String ipBroker, MessageListener messageListener) {
final String brokerUrl = "tcp://" + ipBroker;
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
try {
Connection connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(transacted, ackMode);
Destination mainTopic = session.createTopic(clientQueueName);
//Setup a message producer to send message to the queue the server is consuming from
producer = session.createProducer(mainTopic);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
MessageConsumer responseConsumer = session.createConsumer(mainTopic);
responseConsumer.setMessageListener(messageListener);
} catch (JMSException e) {
e.printStackTrace();
}
}
public static TransactionBuilder withLocation(String ipBroker) {
return new TransactionBuilder(ipBroker);
}
public void send(String msg) throws JMSException {
producer.send( session.createTextMessage(msg) );
}
public void send(java.io.Serializable o) throws JMSException {
producer.send( session.createObjectMessage(o) );
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment