Created
August 20, 2011 23:07
-
-
Save solo-seven/1159806 to your computer and use it in GitHub Desktop.
Here is a RESTful service that uses JPA and JMS within the same transaction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GET | |
@Path("/load") | |
public String load() { | |
try { | |
LOG.debug("Getting Initial Context and looking up resources."); | |
Context ic = new InitialContext(); | |
XAConnectionFactory xacf = (XAConnectionFactory) ic.lookup("XAConnectionFactory"); | |
UserTransaction ut = (UserTransaction) ic.lookup("UserTransaction"); | |
XAConnection conn = xacf.createXAConnection(); | |
Queue workQueue = (Queue) ic.lookup("queue/WorkQueue"); | |
LOG.debug("Starting JMS session"); | |
Session session = conn.createXASession(); | |
MessageProducer producer = session.createProducer(workQueue); | |
LOG.debug("Starting transaction"); | |
ut.begin(); | |
EntityManager em = ((EntityManagerFactory) ic.lookup("java:comp/env/persistence/em")).createEntityManager(); | |
Account account = new Account(); | |
account.setName("Test Name"); | |
LOG.debug("Persisting account"); | |
em.persist(account); | |
TextMessage msg = session.createTextMessage("Test Message."); | |
LOG.debug("Sending text message to JMS"); | |
producer.send(msg); | |
LOG.debug("Committing transaction"); | |
ut.commit(); | |
LOG.debug("Closing everybody"); | |
producer.close(); | |
session.close(); | |
conn.close(); | |
return "TRUE"; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return "FALSE"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment