Skip to content

Commit

Permalink
add properties
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanov committed Feb 18, 2022
1 parent 59cad14 commit 60b8b2f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ $ ./gradlew bootRun --args='--messaging.message="Hello, World!" --messaging.queu
```
Simple Java application for JMS messaging in Artemis
--broker-url set broker url (default [tcp://127.0.0.1:61616])
--broker-username set broker url (default [admin])
--broker-password set broker password (default [admin])
--messaging.queue-name set queue name (default 'my-queue')
--messaging.message set message text (default 'hello, world + timestamp')
--help print help message
--broker-url set broker url (default [tcp://127.0.0.1:61616])
--broker-username set broker url (default [admin])
--broker-password set broker password (default [admin])
--messaging.queue-name set queue name (default 'my-queue')
--messaging.message set message text (default 'hello, world + timestamp')
--messaging.properties.<key>=<value> set properties
--help print help message
```

## Consume from Artemis UI
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/ru/romanow/jpa/HelpPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ public final class HelpPrinter {
public static final String PASSWORD = "broker-password";
public static final String QUEUE_NAME = "messaging.queue-name";
public static final String MESSAGE = "messaging.message";
public static final String PROPERTIES = "messaging.properties.<key>=<value>";

public static void print() {
System.out.printf(
"Simple Java application for JMS messaging in Artemis\n\n" +
"--%-24s set broker url (default [tcp://127.0.0.1:61616])\n" +
"--%-24s set broker url (default [admin])\n" +
"--%-24s set broker password (default [admin])\n" +
"--%-24s set queue name (default 'my-queue')\n" +
"--%-24s set message text (default 'hello, world + timestamp')\n" +
"--%-24s print help message\n",
BROKER_URL, USERNAME, PASSWORD, QUEUE_NAME, MESSAGE, HELP
"--%-36s set broker url (default [tcp://127.0.0.1:61616])\n" +
"--%-36s set broker url (default [admin])\n" +
"--%-36s set broker password (default [admin])\n" +
"--%-36s set queue name (default 'my-queue')\n" +
"--%-36s set message text (default 'hello, world + timestamp')\n" +
"--%-36s set properties\n" +
"--%-36s print help message\n",
BROKER_URL, USERNAME, PASSWORD, QUEUE_NAME, MESSAGE, PROPERTIES, HELP
);
}
}
36 changes: 27 additions & 9 deletions src/main/java/ru/romanow/jpa/JmsApplication.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.romanow.jpa;

import lombok.Data;
import lombok.SneakyThrows;
import org.apache.activemq.artemis.jms.client.ActiveMQQueue;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
Expand All @@ -14,11 +15,15 @@
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.TextMessage;
import java.util.Map;

import static java.time.LocalDateTime.now;
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME;
import static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.util.StringUtils.hasLength;
import static ru.romanow.jpa.HelpPrinter.*;
import static ru.romanow.jpa.HelpPrinter.HELP;
import static ru.romanow.jpa.HelpPrinter.print;

@SpringBootApplication
@EnableConfigurationProperties(JmsApplication.MessagingProperties.class)
Expand All @@ -37,23 +42,30 @@ public static void main(String[] args) {
SpringApplication.run(JmsApplication.class, args);
}

@JmsListener(destination = "my-queue")
public void receiver(String content) {
logger.info("Incoming message from queue [{}]: [{}]", QUEUE_NAME, content);
}
// @JmsListener(destination = "my-queue")
// public void receiver(String content) {
// logger.info("Incoming message from queue [{}]: [{}]", QUEUE_NAME, content);
// }

@Bean
public ActiveMQQueue topic(MessagingProperties properties) {
return ActiveMQQueue.createQueue(properties.queueName);
}

@Bean
public ApplicationRunner sender(MessagingProperties properties, ActiveMQQueue queue, JmsTemplate jmsTemplate) {
public ApplicationRunner sender(MessagingProperties messagingProperties, ActiveMQQueue queue, JmsTemplate jmsTemplate) {
return args -> {
while (true) {
logger.info("Send message to queue [{}]", properties.queueName);
final String message = getMessage(properties);
jmsTemplate.send(queue, session -> session.createTextMessage(message));
logger.info("Send message to queue [{}]", messagingProperties.queueName);
final String text = getMessage(messagingProperties);
jmsTemplate.send(queue, session -> {
final TextMessage message = session.createTextMessage(text);
final Map<String, String> properties = messagingProperties.getProperties();
if (properties != null && !properties.isEmpty()) {
properties.forEach((name, value) -> setProperty(message, name, value));
}
return message;
});
Thread.sleep(TIMEOUT);
}
};
Expand All @@ -66,11 +78,17 @@ private String getMessage(MessagingProperties properties) {
: "Hello queue world " + now().format(ISO_LOCAL_DATE_TIME);
}

@SneakyThrows
private void setProperty(TextMessage msg, String name, String value) {
msg.setObjectProperty(name, value);
}

@Data
@ConfigurationProperties(prefix = "messaging")
public static class MessagingProperties {
private String queueName;
private String message;
private Map<String, String> properties;
}
}

0 comments on commit 60b8b2f

Please sign in to comment.