activemq-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Peter Robbins (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (ARTEMIS-721) ObjectInputStreamWithClassLoader#resolveClass0 uses Thread.currentThread().getContextClassLoader()
Date Thu, 08 Sep 2016 14:58:20 GMT

     [ https://issues.apache.org/jira/browse/ARTEMIS-721?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Peter Robbins updated ARTEMIS-721:
----------------------------------
    Description: 
ObjectInputStreamWithClassLoader#resolveClass0 uses Thread.currentThread().getContextClassLoader()
to resolve the the class of a message body when using the JMS 2.0 API to get a message body.

message.getBody(MyCoolObject.class) will throw javax.jms.MessageFormatException "Body not
assignable to class..." in an OSGi environment. There should be a way to configure which classloader
is used to resolve message objects.

Workaround:
Swap out the currentThread context classloader

{code}
public class MyCoolMessageListener implements MessageListener {
	private static final Logger log = LogManager.getLogger();

	@Override
	public void onMessage(Message message) {
		final ClassLoader loader = Thread.currentThread().getContextClassLoader();
		try {
			Thread.currentThread().setContextClassLoader(MyCoolObject.class.getClassLoader());
			MyCoolObject messageBody = message.getBody(MyCoolObject.class);
			message.acknowledge();
		} catch (JMSException e) {
			log.error(e.getMessage(), e);
		} finally {
			Thread.currentThread().setContextClassLoader(loader);
		}
	}
}
{code}

This sort of defeats the purpose of the simpler, more readable, JMS 2 API though.

  was:
ObjectInputStreamWithClassLoader#resolveClass0 uses Thread.currentThread().getContextClassLoader()
to resolve the the class of a message body when using the JMS 2.0 API to get a message body.

message.getBody(MyCoolObject.class) will throw javax.jms.MessageFormatException "Body not
assignable to class..." in an OSGi environment. There should be a way to configure which classloader
is used to resolve message objects.

Workaround:
Swap out the currentThread context classloader

{code}
public class MyCoolMessageListener implements MessageListener {
	private static final Logger log = LogManager.getLogger();

	@Override
	public void onMessage(Message message) {
		final ClassLoader loader = Thread.currentThread().getContextClassLoader();
		try {
			Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
			MyCoolObject messageBody = message.getBody(MyCoolObject.class);
			message.acknowledge();
		} catch (JMSException e) {
			log.error(e.getMessage(), e);
		} finally {
			Thread.currentThread().setContextClassLoader(loader);
		}
	}
}
{code}

This sort of defeats the purpose of the simpler, more readable, JMS 2 API though.


> ObjectInputStreamWithClassLoader#resolveClass0 uses Thread.currentThread().getContextClassLoader()
> --------------------------------------------------------------------------------------------------
>
>                 Key: ARTEMIS-721
>                 URL: https://issues.apache.org/jira/browse/ARTEMIS-721
>             Project: ActiveMQ Artemis
>          Issue Type: Bug
>          Components: osgi
>    Affects Versions: 1.3.0
>         Environment: JDK 1.8.0_91, OS X 10.11.6, Apache Felix 5.0.1
>            Reporter: Peter Robbins
>
> ObjectInputStreamWithClassLoader#resolveClass0 uses Thread.currentThread().getContextClassLoader()
to resolve the the class of a message body when using the JMS 2.0 API to get a message body.
> message.getBody(MyCoolObject.class) will throw javax.jms.MessageFormatException "Body
not assignable to class..." in an OSGi environment. There should be a way to configure which
classloader is used to resolve message objects.
> Workaround:
> Swap out the currentThread context classloader
> {code}
> public class MyCoolMessageListener implements MessageListener {
> 	private static final Logger log = LogManager.getLogger();
> 	@Override
> 	public void onMessage(Message message) {
> 		final ClassLoader loader = Thread.currentThread().getContextClassLoader();
> 		try {
> 			Thread.currentThread().setContextClassLoader(MyCoolObject.class.getClassLoader());
> 			MyCoolObject messageBody = message.getBody(MyCoolObject.class);
> 			message.acknowledge();
> 		} catch (JMSException e) {
> 			log.error(e.getMessage(), e);
> 		} finally {
> 			Thread.currentThread().setContextClassLoader(loader);
> 		}
> 	}
> }
> {code}
> This sort of defeats the purpose of the simpler, more readable, JMS 2 API though.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message