I am using WildFly 19 and a JMS topic application. After running this program it returns a null
value.
try {
final Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
env.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");
env.put(Context.SECURITY_PRINCIPAL, "jmsuser");
env.put(Context.SECURITY_CREDENTIALS, "Password1!");
namingContext = new InitialContext(env);
ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.lookup("jms/RemoteConnectionFactory");
Destination destination = (Destination) namingContext .lookup("jms/topic/myTopic");
context = connectionFactory.createContext("jmsuser", "Password1!");
context.createProducer().send(destination, "mymeninsgh city topic !");
System.out.println("Sent message successuflly ");
JMSConsumer consumer = context.createConsumer(destination);
String text = consumer.receiveBody(String.class, 5000);
System.out.println("Received message with content " + text);
} catch (Exception e) {
System.out.println(e.getMessage());
throw e;
} finally {
if (namingContext != null) {
namingContext.close();
}
if (context != null) {
context.close();
}
}
WildFly topic configuration is :
After run this program it gives bellow error:
Sent message successuflly
Received message with content null
How can solve this error (null
)? How can I get actual value instead of null
?
What you're seeing is the expected behavior since you're sending the message to a JMS topic before creating a consumer. This is because JMS topics follow publish/subscribe semantics which dictate that messages sent before any subscriptions are created get discarded.
You should either: