Friday, October 1, 2010

EJB.next Connector/Bean API : JAX-RS and beyond

It isn't commonly known that MessageDrivenBeans (MDBs) are not directly tied to the Java Message Service (JMS). In fact, they are tied to the Java EE Connector Architecture. It's even less commonly known that MDBs are not necessarily asynchronous. It's really the Connector that drives the communication style.
Overall, it is a very cool model that does allow for some pretty impressive and standard extension to any compliant Java EE platform. It is, however, incredibly underused. With a few changes to the model, it could be made to support even things like JAX-RS. Let's break it down.
The touchpoints between the Connector and the MDB are the ActivationSpec/ActivationConfig and the MessageListener interface. Using a fictitious "Email" Connector idea, let's see how this looks.
EmailConsumer (MessageListener) interface*
package org.superemail.connector;

// other imports...
    
public interface EmailConsumer {
    public void receiveEmail(Properties headers, String body);
}
EmailAccountInfo (ActivationSpec) class
package org.superemail.connector;
    
/**
 * This class is basically an old-style JavaBean with get/set for each property
 */
public class EmailAccountInfo implements javax.resource.spi.ActivationSpec {

    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void validate() throws InvalidPropertyException {
    }
}
That's it for the Connector (aside from the Connector itself). I have left out a little detail on the ActivationSpec, we'll cover that later.
Now for the MDB's side of things. The MDB needs to implement the Connector's EmailConsumer interface and configure the Connector'sEmailAccountInfo JavaBean which is done via the activation-config tag of the ejb-jar.xml or via @ActivationConfigPropertyannotations in the @MessageDriven declaration.
@MessageDriven(activationConfig = 
        {@ActivationConfigProperty(
                propertyName = "address", 
                propertyValue = "dblevins@apache.org")
        })
public class EmailBean implements EmailConsumer {

    @PostConstruct
    public void init() {
    }

    public void receiveEmail(Properties headers, String body) {
        // do your thing!
    }
}
Done. Those are the basics. The Connector supplies a MessageListener interface and an ActivationConfig JavaBean, the MDB implements the interface and configures the JavaBean via the loosely-typed @ActivationConfigProperty.
There are a few things that prevent this model from reaching its true potential:
  • Metadata is loosely typed in the bean code
  • Only class-level metadata is allowed, not method-level
  • Requiring an interface can limit expressiveness
Let's see how life might look if we eliminate the JavaBean and allow the Connector to instead supply an annotation.
package org.superemail.connector;
    
@Target(TYPE)
@Retention(RUNTIME)
@javax.resource.annotation.ActivationSpec
public @interface EmailAccountInfo {
    String address();
}
Side Note: The original javax.resource.spi.ActivationSpec interface has a 'validate()' method on it to validate the JavaBean. A clear update to that part of the API would be to instead use the Bean Validation API.
Which gives us a bean that might look like this:
@MessageDriven
@EmailAccountInfo(address = "dblevins@apache.org")
public static class EmailBean implements EmailConsumer {

    @PostConstruct
    public void init() {
    }

    public void receiveEmail(Properties headers, String body) {
        // do your thing!
    }
}
Now we're getting somewhere!
Ok, let's get rid of that message listener interface and image our Email Connector uses a very JAX-RS inspired API for consuming emails. If you know a little JAX-RS you'll see where I'm going with this.
@MessageDriven
@EmailAccountInfo(address = "dblevins@apache.org")
public static class EmailBean {

    @PostConstruct
    public void init() {
    }

    @Deliver @Header("Subject: {subject}")
    public void receiveEmail(@HeaderParam("subject") String subject, @Body String body) {
        // do your thing!
    }
}
Pretty neat, huh?
Side note: For the rare few of you that speak Connector implementation, instead of giving your Resource Adapter a MessageEndpoint that is a proxy that only implements the MessageListener interface and the standard MessageEndpoint interface, the Container would instead give you an @LocalBean proxy that also implements MessageEndpoint. If you're thinking that an API like that would be great to use... imagine with the above changes you could make that API ... and use it in any compliant Java EE platform. Maybe even submit your own JSR if you came up with something great.
Truthfully speaking, it's the Connector who controls the lifecycle of the bean (MDB). The Connector API already allows for the Connector to say to the Container, "create me a bean" and "destroy this bean". So really, we don't need @MessageDriven anymore. Instead we can do this:
import javax.annotation.ManagedBean;

@ManagedBean
@EmailAccountInfo(address = "dblevins@apache.org")
public static class EmailBean {
 // ....
}
That's a little more modern and perhaps a bit clearer.
At this point, we have a generic API to hook arbitrary "Connectors" up to arbitrary "Beans" that are managed by a container. Were this the case when JAX-RS was created, they wouldn't have had to put so much effort into duplicating all the services that people who have managed beans expect: injection of resources, lifecycle callbacks, interceptors.
Summary: A few improvements to the expressiveness of the metadata passed between a Bean and its Connector could kick the MDB/Connector model into the mainstream as was the intention of the API from the beginning. New specifications could be created based on this model and be introduced incrementally as they are developed and needed.
The impact of fully opening up the Bean metadata to the Connector is quite deep and wide and likely not something that will scream at you immediately. Ask yourself, could something like the new EJB 3.1 @Schedule API have been done with a Connector/Bean model like this? To some extent probably it could. Certainly there already existed a few Quartz Connectors out there prior to the introduction of @Schedule.
Take a moment to daydream. What kind of Connectors could you create with this model?


UPDATE - Feb 7th, 2013

Save this proposal.  Without more voices it will be delayed till Java EE 8.  Let's not wait 4 years!  Take action and celebrate the Java Community Process openness by showing your support for including this in Java EE 7.  Act now.

VOTE AND COMMENT HERE http://java.net/jira/browse/EJB_SPEC-60




619 comments:

«Oldest   ‹Older   601 – 619 of 619
william james said...

Looking for top quality Perdisco Assignment Help to impress your professors? Most students find it challenging. EssayCorp Having expert writers to help you on your Perdisco assignment.
Perdisco assignment help

kamalcargo said...

Great stuff, you helped me out so much! thanks for sharing.
packers and movwers thane

Nisha sharma said...

Ahmedabad Escorts - Punjabi Girl. We transport your vision, top south Indian Superstar high profile models and air hostess for Escorts Service in Ahmedabad.
russian ahmedabad escorts ###
Call Girls Ahmedabad ###
vadodara call girls ###
surat call girls ###
nashik call girls ###
ambawadi call girls ###

Richa Malik said...

I am an ordinary for each client of your blog. The blog is amazingly animating and will be much significant for us. I really cherished particularly with this article here. Amazingly it's a shocking article that I had ever examined. Delhi Escorts I believe it will help a ton for all of the understudies. Thankful to you such an incredible sum for these dumbfounding posts and please keep update like this astonishing article. Escorts in Delhi

Sonia Mehra said...

How toBook Escorts in Hyderabad
If you are type person, who prefers high-class escorts girls.
then,
Here some step to choose high profile escort girls in Hyderabad
1. Just visit our website
2. Enjoy the pic of most sexy escorts
3. select any of our best
4. then, call "9347773494" and books 
Enjoy the ride there desirable Hyderabad escorts girls.

Richa Malik said...

This is really incredible news for health and prosperity customers. High Profile Escorts in Delhi In case you have to oblige your body, by then Buy Supplement in Pakistan visit our webpage and grab an online upgrades which gives wide extent of dietary improvements.

goa escorts service said...

Hi firends My Name Is sophia shekh And I Am 24yrs Old Escort Girl Living In Goa And Here Is An Independent Escorts Girls Service In Goa Escorts So u Can Call Us And

Book An Call girls Get your Doorstep.
Goa Escorts
Goa Call Girls
Escorts In Goa
Call Girls In Goa
https://sophiagoaescorts.com/

Myrussian said...

Escorts Services in Kolkata We offers the best facility to the clients to make fun with a hot female a Kolkata escort Call-+91-8420572529.
Kolkata Escort
Kolkata Escorts
Kolkata Female Escort
Kolkata Call Girls
Kolkata Independent Escort
Kolkata Escorts Girls
Kolkata Call Girl

Myrussian said...

Are you looking for Russian Escort in Kolkata or some fun with our Russian Call Girls in Kolkata, if yes then you have ended at right place available Foreigner Call Girls 24/7 Call-+91-8420572529.

Russian Escorts in Kolkata
Foreigner Escorts in Kolkata
Russian Kolkata Escorts
Russian model Escorts Agency
Kolkata Russian Escort
Kolkata Escorts Girls
Foreigner Call Girls Kolkata

Myrussian said...

Our Models Escorts Service offer genuine and high profile Models call girls in Kolkata.Call us: +91-8420572529. We have the top Class Models.

Models Call Girls in Kolkata
Models Escort in Kolkata
Kolkata Models Esocrts
Kolkata model Escort
Kolkata Russian Escort
Kolkata independent Models escorts
Kolkata models escorts services
Kolkata models escorts services

Kolkata models escorts services

Neha Tondon said...

High profile Pune Independent Escorts are hired for high class elite where they want to have these escorts for satisfying their sexual desires.

Neha Tondon said...

As a part of a Escorts in Pune famous for delivering quality and endless service. I can assure you that your sexual desires are fulfilled if you are willing to spend a night with me.

gyaan said...




Hi Profile Escorts in Haridwar are considered to be the best and suitable girls for such needs. They offer crowning escort services which come very close to their individual needs. During the sex, high-class escorts remain very bold, energetic and never underestimates her customer. Check our other srevices...
Call Girls in Surat
Russian Escorts in Haridwar
Call Girls in Gurgaon
Escorts in Pune
Call Girls in Mahipalpur

Vaishali Verma said...

They are quite affordable escorts available in Dehradun. Mostly gentlemen prefer quality instead of quantity in their life, Therefore we are here to present some Escorts Girls in Dehradun who are having effervescent qualities.
Dehradun escorts
Haridwar escorts
Rishikesh escorts
Mussoorie escorts

Vaishali Verma said...

You want the beautiful woman you've desired in magazines or lingerie and swimsuit catalogues and now suddenly they are in front of your eyes. So enjoy your trip with heavenly joys with Best Escort Services in Udaipur.
Udaipur escorts

Midnight Partner said...

Best and Top Interior Designers in Gurgaon If you are confounded? Try not to stress.
Best Interior Designers In Delhi
We offer a free conference to help you in settling on a choice with the best vision and quality that you merit.

mansisingh said...

Welcome to best Delhi escorts agency as we think that you are hunting down Delhi ... These women are so particularly more than simply attractive call girls.
Visit my website : -
delhi escorts
delhi call girls
call girls in delhi
delhi call girl
independent delhi escorts
call girls in delhi

Nisha sharma said...

Premium & VIP Ahmedabad Escorts are available at karnavati escorts agency. There has hot & sexy Ahmedabad escorts are coming just by one call.
Call Girls Ahmedabad ###
vadodara call girls ###
surat call girls ###
nashik call girls ###
ambawadi call girls ###
ambli call girls ###
amraiwadi call girls ###
bodakdev call girls ###

xyz said...

We are working as the Best Digital Marketer in the USA. We have experts who give many services to our clients related to the digital marketing. website development comapny in usa

«Oldest ‹Older   601 – 619 of 619   Newer› Newest»