jsr343-experts@jms-spec.java.net

[jsr343-experts] Re: (JMS_SPEC-33) Improving the JMS API with API simplifications, annotations and CDI

From: Nigel Deakin <nigel.deakin_at_oracle.com>
Date: Mon, 25 Jul 2011 20:46:40 +0100

Reza,

Thank you for your helpful reply. I'd welcome any other responses to my initial message - as I said I don't want to lose
sight of what problems we're trying to solve.

But I do have some questions about your suggestions below - most of them still high-level, along the lines of "what
problems are we trying to solve, and to what extent does this help us solve them".

I'd like to work towards getting some proposals together for the EG to consider. Can you help me with this? I'll contact
you directly.

Nigel


On 22/07/2011 00:04, Reza Rahman wrote:
> Nigel,
>
> Firstly, I'm glad we are finally getting to this. You initial analysis/approach looks very sound I think.
>
> Below are specific responses. I am trying to be brief on purpose to try to respect readers' time. So if anything
> requires further explanation, please let me know and I am more than happy to follow-up as needed. With all due respect,
> I'll also ignore some of the questions you posed because I do not think they require a direct answer at this point. If I
> am mistaken, please do tell me so.
>
> I do not think it is necessary to change the semantics of the existing JMS API too much. It is a compact, successful,
> good general purpose API. It is also possible to solve all the issues you pointed out simply via CDI because CDI is
> designed to solve the exact problems we are talking about -- namely eliminating boilerplate code while still maintaining
> a high degree of flexibility.
>
> The best way to see why is unfortunately by diving into the details. As an aside, we initially thought the Seam JMS
> Module was very promising in demonstrating how this could be done but seemed to have gotten a bit hazy somewhere along
> the road (granted the module is still being actively evolved). That is what forced us to come up with our own design for
> Resin (that being said perhaps we should have tried to provide direct feedback to the Seam JMS Module team as well; in
> addition, I do understand that the project is driven by community volunteers and not dedicated commercial engineering
> staff).
>
> Let's take your message send code as an example. With CDI, that code could look like the following:
>
> @Inject @JmsConnectionFactory("jms/MyConnectionFactory") Session session;
> @Inject @JmsDestination("jms/MyQueue") MessageProducer producer;
>
> @Transactional
> private void send(String messageText) {
> Message message = session.createTextMessage(messageText);
> producer.send(message);
> }

We're only using the Session object here because it has the factory methods that create Message objects, even though the
resulting Message objects may be used by any Session. However in the first @Inject statement, the Session object is the
one which we're using to define the connection factory. This strikes me as being potentially confusing for anyone who is
not already familiar with the underlying API and who doesn't already know that the session is, under the covers, also
being used to create a producer, and it is the producer which is actually linked to the connection factory. We've
reduced the number of lines of code, but have we actually made the API easier to understand?

I presume @Transactional means a local transaction. When is the session being committed?

In a Java EE application we'd normally not be able to use @Transactional (which I presume means a local transaction) but
would use the standard CMT/BMT mechanisms to commit the transaction.

> If we really wanted to get crazy about it, we could even inject the message to be sent. The code would be like this:
>
> @Inject @JmsConnectionFactory("jms/MyConnectionFactory") TextMessage message;
> @Inject @JmsDestination("jms/MyQueue") MessageProducer producer;
>
> @Transactional
> private void send(String messageText) {
> message.setText(messageText);
> producer.send(message);
> }

This hides the factory methods that creates a Message from a Session, but still expects the application to specify a
connection factory in the declaration of the Message. I know why, of course, but if we were following this approach I
think the goal should be to allow a Message to be injected without the need to define a conneciton factory.

>
> We can make all of this happen with CDI alone. The only change in the basic JMS API that would be required would be
> getting rid of the checked exceptions that I don't think add much value in the first place.

As I wrote in my email, the need to handle JMSException everywhere complicates the API. However I'm not sure what scope
we have to remove checked exceptions from the existing JMS API without introducing an incompatible change. (Suggestions
welcome).

> Now, with CDI you could also
> inject any intermediate JMS object that you wish. So you could inject connection factories, connections, connection
> meta-data, message consumers, queue browsers, temporary queues, temporary topics and so on. The CDI runtime would be
> aware of all of the intermediate objects anyway so it would be trivial to make them available for injection/resolve them
> from the correct scope.

I think the key to the success of this would be whether we are actually removing the need for the user to see some of
these intermediate objects, or whether we are simply hiding the code that creates one object from another.

>
> Things can be just as simple/powerful for asynchronous message receipt:
>
> public class SomeBean {
> ...
> public void listenToSomeMessage(@Observes @JmsDestination("jms/SomeQueue") Message someMessage)
> ...
> }
>
> Or at an even higher level of abstraction:
>
> public class SomeBean {
> ...
> public void listenToSomeMessage(@Observes @JmsDestination("jms/SomeQueue") String messageText)
> ...
> }

As you know, we couldn't offer this in a Java EE application without changing the platform restrictions that prevent
applications creating async listeners. Essentially, the JMS API in an app server doesn't have a method
setMessageListener(). This makes your suggestion here different from your earlier examples which would you could think
of as just a layer on top of the existing JMS API, and so we should perhaps consider this separately.

>
> Again, this would require no changes to the basic JMS API.
>
> If an implementer wants to make the higher level abstractions available in Java SE, they can easily do this themselves.
> If we absolutely insist that this is necessary, it would be easy to get the CDI EG to provide better support for Java SE
> since they are going in that direction anyway. None of the underlying capabilities required to support the abstractions
> are really Java EE specific. For example, we plan on making all of this possible in Java SE via our CDI-based embedded
> container. For those familiar with Spring and not embedded containers in the CDI/Java EE realm, this is basically the
> same as what Spring does in Java SE.
>
> Not being able to define JMS resources in a standard way is a problem. This is especially the case since it is possible
> to do so today for JDBC resources: http://blogs.oracle.com/Lance/entry/introducing_the_datasourcedefinition_annotation.
> I imagine we can solve this once the JCA pluggability issue is resolved properly. We can have something like
> @QueueDefinition, @TopicDefinition, @ConnectionFactoryDefinition and so on similar to @DataSourceDefinition.

Can you say a bit more about what you see as the problem here?

I've been having some discussions with the Java EE platform spec leads about improving the ways that resources are
defined in Java EE applications.

Nigel

>
> Again, I am happy to follow up on any of this if it is needed.
>
> Cheers,
> Reza
>
>
> On 7/21/2011 1:00 PM, Nigel Deakin wrote:
>> I logged the following issue:
>> http://java.net/jira/browse/JMS_SPEC-33
>>
>> This is a placeholder for this goal I proposed some time ago for JMS 2.0, and which received support from EG members:
>> "We should provide a modern, easier-to-use API, probably using annotations and CDI. There seemed to be a variety of
>> ideas of how we might do this, and we will need to explore these in detail. Whilst I think we mostly see this as a
>> feature for Java EE containers, there seems to be interest in offering features to those using a Java SE environment
>> as well."
>>
>> There has been some discussion already about this, but I'd like to go back a few steps to discuss what we are trying
>> to achieve, and how we might achieve it, and where technologies like annotations and CDI might fit in.
>>
>> One of the reasons I'm doing this is that although we have some Java language and CDI experts in this EG, I know from
>> talking to individuals that the level of knowledge in this group is variable. I hope the "experts" take this as a hint
>> that they should be prepared to help teach the others...
>>
>> Also, I want to avoid us saying "CDI is the solution, now what's the problem" and, in particular, make sure that we
>> don't simply use CDI as a way to hide the complexities of the JMS API when we could be eliminating those complexities,
>> not hiding them.
>>
>> I also think we need to discuss the extent to which any improvements could be offered to Java SE applications as well
>> as those running Java EE. Although it is tempting to instinctively define our goal as offering the same improvements
>> to both, I think there's much greater scope to offer improvements to Java EE applications, given that Java EE is by
>> definition intended to make application development simpler and already offers a significantly simpler experience of
>> using JMS than Java SE. At the every least, the different nature of the two environments will by definition mean the
>> way that JMS is used will always be different in each.
>>
>> One thing we should bear in mind is that the JMS API wasn't really designed with Java EE in mind. Java EE already
>> imposes significant restrictions on how the API can be used, and we might want to consider whether this offers
>> opportunities to simplify the API for such users.
>>
>> WHAT'S THE PROBLEM WITH THE JMS API?
>> ------------------------------------
>>
>> I'd like to start with discussion of the API and what is wrong with it, before we get on to possible solutions. What
>> are the main problems with the JMS API at present which make it more difficult to use than it should?
>>
>> I think the starting point is that the JMS API is rather more cumbersome than it need to be, with a lot of boilerplate
>> code needed to send or receive a single message. Here is how the existing JMS spec expects you to write a method which
>> sends a String:
>>
>> private void send(String messageText) throws NamingException, JMSException {
>> Properties props = new Properties();
>> InitialContext ic = new InitialContext(props);
>> ConnectionFactory connectionFactory = (ConnectionFactory) ic.lookup("myConnectionFactory");
>> Connection connection = connectionFactory.createConnection();
>> try {
>> Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
>> Destination destination = (Destination) ic.lookup("MyQueue");
>> MessageProducer producer = session.createProducer(destination);
>> Message message = session.createTextMessage(messageText);
>> producer.send(message);
>> } finally {
>> if (connection!=null) connection.close();
>> }
>> }
>>
>> That is indeed a lot of boilerplate code (i.e. code that has to be included in many places with little or no
>> alteration). It also throws two Exceptions which need to be handled somehow. In addition the administrator is expected
>> to use provider-specific tools to bind suitable Connection and Destination objects in JNDI.
>>
>> In a Java EE container, resource injection allows things to be simplified a little by removing the need to create an
>> InitialContext and replacing the JNDI lookups with @Resource annotations. This means that NamingException no longer
>> needs to be explicitly handled.
>>
>> @Resource(name="myQueue") Destination destination;
>> @Resource(name="myConnectionFactory") ConnectionFactory connectionFactory;
>>
>> private void send(String messageText) throws JMSException {
>> Connection connection = connectionFactory.createConnection();
>> try {
>> Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
>> MessageProducer producer = session.createProducer(destination);
>> Message message = session.createTextMessage(messageText);
>> producer.send(message);
>> } finally {
>> if (connection!=null) connection.close();
>> }
>> }
>>
>> This still requires a JMSException to be handled, and the administrator is still expected to use provider-specific
>> tools to bind suitable Connection and Destination objects in JNDI.
>>
>> Almost identical issues arise if you write code to synchronously consume a message. Let's leave asynchronously
>> consuming messages (via MessageListeners and MDBs until a little later).
>>
>> So, what are the problems here?
>>
>> PROBLEMS
>> --------
>>
>> 1. The need to create several intermediate objects which we might use only once and not use again. In the above
>> example, the Connection, Session, MessageProducer and TextMessage objects will never be used again after this method
>> returns (though in other scenarious they might).
>>
>> 2. The need to tidy up resources after use. In practice, this means calling Connection.close() after use, ideally in a
>> finally block. Whether this physically releases resources or simply returns the connection to a pool, if we don't call
>> this method we will eventually run out of either resources or connections in the pool.
>>
>> 3. The rather redundant arguments to connection.createSession(). In a Java SE container we have two arguments when one
>> would do, and in a Java EE EJB or web container they're ignored.
>>
>> 4. The dependence on JNDI as a means to isolate provider-specific connection factory and destination configuration
>> from the portable application (which is currently a fundamendal fature of JMS, so this dependence is currently by
>> design).
>>
>> EXISTING DIFFERENCES BETWEEN JMS API in SE and EE
>> -------------------------------------------------
>>
>> Although it is desirable to keep the JMS API the same when in a Java SE and a Java EE environment, I think we should
>> recognise that the API within the Java EE EJB or web container is already significantly simpler than the full JMS API
>> and would be easier to simplify further. Here are some of the things that we might be able to simplify in a Java EE
>> environment.
>>
>> (I should point out that I'm just trying to provoke discussion here and not trying to make specific proposals)
>>
>> - Connections (as objects exposed to applications) are of limited use, other than to create a single Session. For
>> example, each Connection is limited to one Session, so there is no point in having separate Connection and Session
>> objects. The methods setClientID(), setExceptionListener() and stop() are forbidden. This leaves start() as just about
>> the only useful method remaining on a Connection (and then only when consuming messages). So do we really need to
>> expose Connections to the application at all? (Note that Java EE connection pooling means the application doesn't need
>> to keep instances around purely to avoid the cost of creating them).
>>
>> - Sessions are also of limited use, other than to create producers and consumers. Transactions are managed by the
>> transaction manager, so we don't need commit() or rollback(). Client acknowledgement is forbidden, so we don't need
>> acknowledge(). This mainly leaves createMessage() and the other factory methods for messages, which have no particular
>> need to be on the Session object at all. So if we move these factory methods somewhere else, do we need to expose
>> Sessions to the application at all? Sessions have one further important function in JMS: the resources of a session
>> may only be used by a single thread at a time. Any change to hide the Session from the application should not break
>> this restriction or prevent the application observing it.
>>
>> - MessageProducers are obviously useful for sending messages. But do we need to expose a MessageProducer object to the
>> application, other than to allow us to call send() once? The main purpose of a MessageProducer, other than
>> representing a tuple of <destination, deliveryMode, priority, timeToLive> is that it defines JMS message order. So we
>> can't make this object disappear, though we can perhaps make it invisible to the application by defining some rules
>> such as when the order of repeated calls to send() is honoured by the JMS provider.
>>
>> - MessageConsumers represent the tuple <destination, messageSelector>, and for durable topic subscriptions
>> <subscriptionName, clientId> as well. For a queue there is no particular difference between one MessageConsumer
>> instance and another (for the same <destination, messageSelector>). However for a topic the object identity of the
>> MessageConsumer is significant in that each instance receives a copy of all the messages received on the topic, so we
>> probably need to continue to expose this object to the application, at least for synchronous message consumption
>> (let's talk about async message consumption separately).
>>
>> - Messages (and their subclasses) are essentially wrappers for the message payload which allow the application to
>> define user-defined message properties which can be used by message selectors, and one or two header properties such
>> as JMSReplyTo and JMSCorrelationID. Whilst these continue to be needed, perhaps we could provide an simpler API which
>> allows a payload to to be passed directly for use by applications which don't need them.
>>
>> Note that since most of the simplfications suggested above are only possible in a Java EE EJB or web container, we
>> would be introducing a different API in Java EE as for Java SE. However it could be argued that we *already* have a
>> different API in a Java EE EJB or web container (with resource injection, 1 session per connection, forbidden methods,
>> connection pooling, etc) but just don't explain this in the JMS spec and javadocs, much to the confusion of
>> application developers.
>>
>> ASYNC MESSAGE CONSUMPTION
>> -------------------------
>>
>> Now let's talk about asynchronous message consumption. We need to consider this separately because this is the feature
>> which *already* has a completely different API in a Java SE environment and in a Java EE EJB or web container (even
>> though the JMS spec doesn't mention this at all).
>>
>> In a Java SE environment (or the Java EE application client container), message may be consumed asynchronously by
>> creating a MessageConsumer as described above, and then calling MessageConsumer.setMessageListener() to register a
>> MessageListener with an onMessage() method.
>>
>> In a Java EE EJB or web container, the use of MessageConsumer.setMessageListener() is explicitly forbidden (Java EE 6
>> Platform Spec section EE.6.7). Instead, the only means provided for asynchronous message consumption is the
>> message-driven bean (MDB). Since this was defined in the EJB spec rather than the JMS spec this is defined
>> declaratively, using either deployment descriptors or annotations. Here's a simple example using annotation:
>>
>> @MessageDriven(mappedName = "jms/inboundQueue")
>> public class NewMessageBean implements MessageListener {
>> public void onMessage(Message message) {
>> System.out.println("Received a message");
>> }
>> }
>>
>> Where "jms/inboundQueue" is the JNDI name where the queue object can be obtained. Additional information, such as the
>> message selector, can be specified using the activationConfig element of the MessageDriven annotation:
>>
>> @MessageDriven(mappedName = "jms/inboundQueue", activationConfig = {
>> @ActivationConfigProperty(
>> propertyName = "messageSelector",
>> propertyValue = "JMSType = 'car' AND color = 'blue'"),
>> @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
>> })
>>
>> The above examples are for GlassFish, and there does appear to be a problem in that the EJB 3.1 spec is distinctly
>> vague about how a MDB is defined. The relevant sections are 5.4.15, 5.4.16 and 5.4.17.1. The only activation config
>> properties defined are acknowledgeMode (only used when transactions are bean-managed, and which must be either
>> Auto-acknowledge or Dups-ok-acknowledge), messageSelector, destinationType (which must be must be either
>> javax.jms.Queue or javax.jms.Topic) and subscriptionDurability (which must be either Durable or NonDurable). But it
>> doesn't specify how the destination is defined or, when subscriptionDurability is Durable, how the subscription name
>> and clientId are defined.
>>
>> The JCA 1.6 spec has some additional guidance, at least for MDBs that use a resource adapter. Section B2 states thatr
>> providers are "strongly encouraged" to provide the properties mentioned above and also destination, subscriptionName
>> and clientId, with destination and destinationType as "required" properties.
>>
>> Whatever else we do, there seems to be a clear need to make these mandatory for JMS MDBs, whether or not they use JCA,
>> so I've logged this separately as http://java.net/jira/browse/JMS_SPEC-30 and we've alreay started discussing the
>> details separately in the thread for that issue.
>>
>> But it seems to me that the existing MDB feature already addresses a lot of the API problems I listed in the section
>> "PROBLEMS" above.
>>
>> 1. There's no creation of intermediate objects which have no real purpose. Indeed no objects are exposed to the
>> application at all apart from the incoming message itself.
>>
>> 2. There's no need for the application to close or tidy up resources after use.
>>
>> 3. There's no need for the user to use the problematical createSession() method
>>
>> 4. The only potential complexity this leaves, of the four I listed above, is the need to define the queue or topic as
>> an administered object in JNDI - though strictly speaking since the configuration properties are not fully
>> standardised it might be possible to avoid even this, if we think it beneficial.
>>
>> What other "problems" with MDBs does this leave? I can think of a few possible ones:
>>
>> * Reliance on MessageListener interface. You can't simply annotate any arbitrary method as the callback.
>>
>> * You can only have one callback per MDB class
>>
>> * Other types of bean such as stateless session beans and singleton beans can't consume messages asynchronously
>>
>> * MDBs can't be used in a Java SE environment (or the Java EE application client). In those environments you're back
>> to using the raw JMS API.
>>
>> * Any others?
>>
>> We can certainly think of how we might make MDBs easier to use or more flexible. We can also think of how the changes
>> currently being considered in the EJB 3.2 EG offer opportunities for generaising MDBs.
>>
>> However it seems to me that MDBs already go a long way to offering a simple API to applications, and that our real
>> priority is to make it easier and simpler to *send* messages and to consume them *synchronously*.
>>
>> CDI
>> ---
>>
>> So far I've not mentioned CDI (JSR 299). This is part of Java EE 6 and is already built-in to a Java EE container, so
>> long as you define a beans.xml file in your application.
>>
>> What does this give us? As I write at the start I'm hoping that other EG members can help share their knowledge on
>> this, but as far as I can see it:
>>
>> 1. Provides a way to offer a simplified API which is simply a wrapper on top of the JMS API, effectively replacing
>> boilerplate factory code with annotations which inject objects.
>>
>> 2. Provides a way to define the "scope" of a object, so that close() can be called automatically when it falls out of
>> scope. Scopes might also allow us to define the circumstances when repeated calls to some new send method use the same
>> MessageProducer, and when they use different MessageProducers (important for message order).
>>
>> 3. Offers an internal async event mechanism which has some similarity with async messages.
>>
>> CDI is not built into Java SE, and the existing CDI spec doesn't require implementations to support Java SE
>> environments, though this may change. However any CDI-based API in Java SE may need to be significantly different from
>> a CDI-based API in Java EE because Java SE does not support resource injection. Also, any API for use in Java SE will
>> need to reflect the additional features available in SE (local transactions, client ack, multiple sessions per
>> connection etc).
>>
>> ANNOTATIONS
>> -----------
>>
>> We shouldn't forget that annotations don't have to use CDI. Just like with MDBs, if we want to pass information to the
>> container using annotations we can simply define the annotations we want.
>>
>> WHAT NEXT?
>> ---------
>>
>> I'm not trying to make any proposals at this stage. The purpose of this email was to raise the issues that I think we
>> need to consider as part of doing that. In particular:
>>
>> 1. What do *you* think needs to be improved in the JMS API?
>>
>> 2. To what extent should we be trying to simplify the plain Java API, especially in a Java EE environment when a
>> significant proportion of it is redundant anyway?
>>
>> 3. How might the use of annotations (not necessarily CDI) make the JMS API simpler?
>>
>> 4. How might CDI make the JMS API simpler?
>>
>> 5. If we do try to hide complexities behind CDI, will users have to be aware of what is happening behind the scenes
>> and occasionally dip into using some of the hidden objects? If so, are we really making this simpler?
>>
>> 6. Is it possible to provide similar benefits to plain Java SE applications as to Java EE applications, or are the
>> environments just too different, bearing in mind the existing differences between JMS in Java EE and Java SE?
>>
>> I look forward to the discussion.
>>
>> Nigel
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> -----
>> No virus found in this message.
>> Checked by AVG - www.avg.com
>> Version: 10.0.1390 / Virus Database: 1518/3778 - Release Date: 07/21/11
>>
>>
>
>