DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • SaaS in an Enterprise - An Implementation Roadmap
  • AI-Driven RAG Systems: Practical Implementation With LangChain
  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • STRIDE: A Guide to Threat Modeling and Secure Implementation

Trending

  • Optimizing Integration Workflows With Spark Structured Streaming and Cloud Services
  • GitHub Copilot's New AI Coding Agent Saves Developers Time – And Requires Their Oversight
  • Why Database Migrations Take Months and How to Speed Them Up
  • The Future of Java and AI: Coding in 2025

Sending delayed JMS Messages

By 
Alexander Radzin user avatar
Alexander Radzin
·
Sep. 22, 10 · Interview
Likes (1)
Comment
Save
Tweet
Share
59.7K Views

Join the DZone community and get the full member experience.

Join For Free

Very often I have had to implement features that have to do something asynchronously in a minute, day, or at 5PM next Monday. Every time I did this, I implemented some serialization mechanism (typically based on DB) and some scheduled task that runs periodically, checks the table and runs tasks that should be executed now.

Sometimes more generic tools were used, for example, Quartz. I couldn't implement such tasks using JMS : the reason is that JMS API does not allow sending delayed messages, i.e. messages that will not be received by subscriber or receiver immediately. Occasionally I found that some JMS implementations have a proprietary implementation for delayed messages. I decided to perform some searches and aggregate this information into one place. Here is a list of the most popular JMS implementations (see wikipedia):

  • Apache ActiveMQ
  • Apache Qpid
  • FUSE Message Broker (enterprise ActiveMQ)
  • Mantaray a P2P JMS implementation
  • OpenJMS, from The OpenJMS Group
  • JBoss Messaging from JBoss
  • HornetQ from JBoss
  • JORAM, from the OW2 Consortium
  • Open Message Queue, from Sun Microsystems
  • Sun Java System Message Queue, from Sun Microsystems, supported version of Open Message Queue
  • Rabbit MQ

Because the JMS API does not define an interface for delayed messages most SMS providers that support this feature implemented it using message properties. You just have to say something like msg.setLongProperty(“DELAY”, delay). Some implementations require casting to a specific class and invocation of a proprietary method. The following table summarizes the differences between implementations of different SMS providers I found.

 

JMS provider Implementation
Oracle AQ msg.setIntProperty(“JMS_OracleDelay”, delay);
JBoss msg.setLongProperty(“JMS_JBOSS_SCHEDULED_DELIVERY”, now + delay);
ActiveMQ msg.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
OpenJMS ((org.exolab.jms.message.MessageImpl)msg).setJMSXRcvTimestamp(now + delay);
BEA Weblogic queueConnection = queueConnectionFactory.createQueueConnection(); QueueSession queueSession = queueConnection.createQueueSession(true, 0); QueueSender queueSender = queueSession.createSender(queue); ObjectMessage jmsMsg = queueSession.createObjectMessage(message); //Casts queueSender to weblogic.jms.extensions.WLMessageProducer interface and set delivery time ((WLMessageProducer) queueSender).setTimeToDeliver(timeToDeliver); queueSender.send(jmsMsg);

Do we have solution for JMS providers that do not have native support of delayed message delivery? Yes, we do. I would like to suggest the following solution.

Send message to special queue. Let’s call it DELAYED_QUEUE. Add the following special properties to the delayed message:

  • JMS_DESTINATION that contains name of queue or topic where this message should be finally delivered.
  • DELIVERY_TIME that contains time stamp in milliseconds (now + delay).

For each enqued delayed message create a scheduled task that that will run once when message should be delivered. This scheduled task will create a JMS receiver with a selector that looks like DELIVERY_TIME < now (where now is the timestamp), receives all expired messages and sends them to the real JMS destination using property JMS_DESTINATION.

The scheduled task may be implemented as resource adapter (JCA):
BootstrapContext ctx;
ctx.getWorkManager().createTimer(). schedule(new DelayedMessageTimerTask(msg),
new Date(now + delay))
This is not ideal solution. It is OK for relatively small number of messages and non persisted JMS destinations. Improvements of this solution are beyond the scope of this article.  

Conclusions

Most popular JMS implementations support delayed delivery of messages. Even if this feature is not supported we can always implement it using additional queue and scheduled task.


Implementation

Opinions expressed by DZone contributors are their own.

Related

  • SaaS in an Enterprise - An Implementation Roadmap
  • AI-Driven RAG Systems: Practical Implementation With LangChain
  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • STRIDE: A Guide to Threat Modeling and Secure Implementation

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!