org.jboss.resource.adapter.jms
public class: JmsManagedConnection [javadoc |
source]
java.lang.Object
org.jboss.resource.adapter.jms.JmsManagedConnection
All Implemented Interfaces:
javax.jms.ExceptionListener, javax.resource.spi.ManagedConnection
Managed Connection, manages one or more JMS sessions.
Every ManagedConnection will have a physical JMSConnection under the
hood. This may leave out several session, as specifyed in 5.5.4 Multiple
Connection Handles. Thread safe semantics is provided
Hm. If we are to follow the example in 6.11 this will not work. We would
have to use the SAME session. This means we will have to guard against
concurrent access. We use a stack, and only allowes the handle at the
top of the stack to do things.
As to transactions we some fairly hairy alternatives to handle:
XA - we get an XA. We may now only do transaction through the
XAResource, since a XASession MUST throw exceptions in commit etc. But
since XA support implies LocatTransaction support, we will have to use
the XAResource in the LocalTransaction class.
LocalTx - we get a normal session. The LocalTransaction will then work
against the normal session api.
An invokation of JMS MAY BE DONE in none transacted context. What do we
do then? How much should we leave to the user???
One possible solution is to use transactions any way, but under the hood.
If not LocalTransaction or XA has been aquired by the container, we have
to do the commit in send and publish. (CHECK is the container required
to get a XA every time it uses a managed connection? No its is not, only
at creation!)
Does this mean that a session one time may be used in a transacted env,
and another time in a not transacted.
Maybe we could have this simple rule:
If a user is going to use non trans:
- mark that i ra deployment descr
- Use a JmsProviderAdapter with non XA factorys
- Mark session as non transacted (this defeats the purpose of specifying
- trans attrinbutes in deploy descr NOT GOOD
From the JMS tutorial:
"When you create a session in an enterprise bean, the container ignores
the arguments you specify, because it manages all transactional
properties for enterprise beans."
And further:
"You do not specify a message acknowledgment mode when you create a
message-driven bean that uses container-managed transactions. The
container handles acknowledgment automatically."
On Session or Connection:
From Tutorial:
"A JMS API resource is a JMS API connection or a JMS API session." But in
the J2EE spec only connection is considered a resource.
Not resolved: connectionErrorOccurred: it is verry hard to know from the
exceptions thrown if it is a connection error. Should we register an
ExceptionListener and mark al handles as errounous? And then let them
send the event and throw an exception?
- author:
< - a href="mailto:peter.antman@tim.se">Peter Antman.
- author:
< - a href="mailto:jason@planet57.com">Jason Dillon
- author:
< - a href="mailto:adrian@jboss.com">Adrian Brock
- version:
$ - Revision: 73443 $
| Constructor: |
public JmsManagedConnection(JmsManagedConnectionFactory mcf,
ConnectionRequestInfo info,
String user,
String pwd) throws ResourceException {
this.mcf = mcf;
// seem like its asking for trouble here
this.info = (JmsConnectionRequestInfo)info;
this.user = user;
this.pwd = pwd;
try
{
setup();
}
catch (Throwable t)
{
try
{
destroy();
}
catch (Throwable ignored)
{
}
JBossResourceException.rethrowAsResourceException("Error during setup", t);
}
}
Create a JmsManagedConnection. Parameters:
mcf -
info -
user -
pwd -
Throws:
ResourceException -
|
| Method from org.jboss.resource.adapter.jms.JmsManagedConnection Summary: |
|---|
|
addConnectionEventListener, associateConnection, cleanup, destroy, getConnection, getInfo, getLocalTransaction, getLogWriter, getManagedConnectionFactory, getMetaData, getSession, getUserName, getXAResource, lock, onException, removeConnectionEventListener, removeHandle, sendEvent, setLogWriter, start, stop, tryLock, unlock |
| Method from org.jboss.resource.adapter.jms.JmsManagedConnection Detail: |
public void addConnectionEventListener(ConnectionEventListener l) {
listeners.addElement(l);
if (log.isTraceEnabled())
log.trace("ConnectionEvent listener added: " + l);
}
Add a connection event listener. |
public void associateConnection(Object obj) throws ResourceException {
//
// Should we check auth, ie user and pwd? FIXME
//
if (!isDestroyed && obj instanceof JmsSession)
{
JmsSession h = (JmsSession)obj;
h.setManagedConnection(this);
handles.add(h);
}
else
throw new IllegalStateException
("ManagedConnection in an illegal state");
}
Move a handler from one mc to this one. |
public void cleanup() throws ResourceException {
if (isDestroyed)
throw new IllegalStateException("ManagedConnection already destroyed");
// destory handles
destroyHandles();
// I'm recreating the lock object when we return to the pool
// because it looks too nasty to expect the connection handle
// to unlock properly in certain race conditions
// where the dissociation of the managed connection is "random".
lock = new ReentrantLock();
}
Cleans up the, from the spec
- The cleanup of ManagedConnection instance resets its client specific
state.
Does that mean that autentication should be redone. FIXME |
public void destroy() throws ResourceException {
if (isDestroyed || con == null) return;
isDestroyed = true;
try
{
con.setExceptionListener(null);
}
catch (JMSException e)
{
log.debug("Error unsetting the exception listener " + this, e);
}
// destory handles
destroyHandles();
try
{
// Close session and connection
try
{
if (info.getType() == JmsConnectionFactory.TOPIC)
{
if (topicSession != null)
topicSession.close();
if (xaTransacted && xaTopicSession != null) {
xaTopicSession.close();
}
}
else if (info.getType() == JmsConnectionFactory.QUEUE)
{
if (queueSession != null)
queueSession.close();
if (xaTransacted && xaQueueSession != null)
xaQueueSession.close();
}
else
{
if (session != null)
session.close();
if (xaTransacted && xaSession != null)
xaSession.close();
}
}
catch (JMSException e)
{
log.debug("Error closing session " +this, e);
}
con.close();
}
catch (Throwable e)
{
throw new JBossResourceException
("Could not properly close the session and connection", e);
}
}
Destroy the physical connection. |
public Object getConnection(Subject subject,
ConnectionRequestInfo info) throws ResourceException {
// Check user first
JmsCred cred = JmsCred.getJmsCred(mcf,subject,info);
// Null users are allowed!
if (user != null && !user.equals(cred.name))
throw new SecurityException
("Password credentials not the same, reauthentication not allowed");
if (cred.name != null && user == null) {
throw new SecurityException
("Password credentials not the same, reauthentication not allowed");
}
user = cred.name; // Basically meaningless
if (isDestroyed)
throw new IllegalStateException("ManagedConnection already destroyd");
// Create a handle
JmsSession handle = new JmsSession(this, (JmsConnectionRequestInfo) info);
handles.add(handle);
return handle;
}
Get the physical connection handler.
This bummer will be called in two situations:
- When a new mc has bean created and a connection is needed
- When an mc has been fetched from the pool (returned in match*)
It may also be called multiple time without a cleanup, to support
connection sharing. |
protected ConnectionRequestInfo getInfo() {
return info;
}
Get the request info for this connection. |
public LocalTransaction getLocalTransaction() throws ResourceException {
LocalTransaction tx = new JmsLocalTransaction(this);
if (log.isTraceEnabled())
log.trace("LocalTransaction=" + tx);
return tx;
}
Get the location transaction for the connection. |
public PrintWriter getLogWriter() throws ResourceException {
//
// jason: screw the logWriter stuff for now it sucks ass
//
return null;
}
Get the log writer for this connection. |
protected JmsManagedConnectionFactory getManagedConnectionFactory() {
return mcf;
}
Get the connection factory for this connection. |
public ManagedConnectionMetaData getMetaData() throws ResourceException {
if (isDestroyed)
throw new IllegalStateException("ManagedConnection already destroyd");
return new JmsMetaData(this);
}
Get the meta data for the connection. |
protected Session getSession() {
if (info.getType() == JmsConnectionFactory.TOPIC)
return topicSession;
else if (info.getType() == JmsConnectionFactory.QUEUE)
return queueSession;
else
return session;
}
Get the session for this connection. |
protected String getUserName() {
return user;
}
Get the user name for this connection. |
public XAResource getXAResource() throws ResourceException {
//
// Spec says a mc must allways return the same XA resource,
// so we cache it.
//
if (!xaTransacted)
throw new NotSupportedException("Non XA transaction not supported");
if (xaResource == null)
{
if (info.getType() == JmsConnectionFactory.TOPIC)
xaResource = xaTopicSession.getXAResource();
else if (info.getType() == JmsConnectionFactory.QUEUE)
xaResource = xaQueueSession.getXAResource();
else
xaResource = xaSession.getXAResource();
}
if (log.isTraceEnabled())
log.trace("XAResource=" + xaResource);
xaResource = new JmsXAResource(this, xaResource);
return xaResource;
}
Get the XAResource for the connection. |
protected void lock() {
lock.lock();
}
|
public void onException(JMSException exception) {
if (isDestroyed)
{
if (log.isTraceEnabled())
log.trace("Ignoring error on already destroyed connection " + this, exception);
return;
}
log.warn("Handling jms exception failure: " + this, exception);
try
{
con.setExceptionListener(null);
}
catch (JMSException e)
{
log.debug("Unable to unset exception listener", e);
}
ConnectionEvent event = new ConnectionEvent(this, ConnectionEvent.CONNECTION_ERROR_OCCURRED, exception);
sendEvent(event);
}
|
public void removeConnectionEventListener(ConnectionEventListener l) {
listeners.removeElement(l);
}
Remove a connection event listener. |
protected void removeHandle(JmsSession handle) {
handles.remove(handle);
}
Remove a handle from the handle map. |
protected void sendEvent(ConnectionEvent event) {
int type = event.getId();
if (log.isTraceEnabled())
log.trace("Sending connection event: " + type);
// convert to an array to avoid concurrent modification exceptions
ConnectionEventListener[] list =
(ConnectionEventListener[])listeners.toArray(new ConnectionEventListener[listeners.size()]);
for (int i=0; i< list.length; i++)
{
switch (type) {
case ConnectionEvent.CONNECTION_CLOSED:
list[i].connectionClosed(event);
break;
case ConnectionEvent.LOCAL_TRANSACTION_STARTED:
list[i].localTransactionStarted(event);
break;
case ConnectionEvent.LOCAL_TRANSACTION_COMMITTED:
list[i].localTransactionCommitted(event);
break;
case ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK:
list[i].localTransactionRolledback(event);
break;
case ConnectionEvent.CONNECTION_ERROR_OCCURRED:
list[i].connectionErrorOccurred(event);
break;
default:
throw new IllegalArgumentException("Illegal eventType: " + type);
}
}
}
|
public void setLogWriter(PrintWriter out) throws ResourceException {
//
// jason: screw the logWriter stuff for now it sucks ass
//
}
Set the log writer for this connection. |
void start() throws JMSException {
con.start();
}
|
void stop() throws JMSException {
con.stop();
}
|
protected void tryLock() throws JMSException {
int tryLock = mcf.getUseTryLock();
if (tryLock < = 0)
{
lock();
return;
}
try
{
if (lock.tryLock(tryLock, TimeUnit.SECONDS) == false)
throw new ResourceAllocationException("Unable to obtain lock in " + tryLock + " seconds: " + this);
}
catch (InterruptedException e)
{
throw new ResourceAllocationException("Interrupted attempting lock: " + this);
}
}
|
protected void unlock() {
lock.unlock();
}
|