You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@activemq.apache.org by "Peter Robbins (JIRA)" <ji...@apache.org> on 2016/09/08 14:58:20 UTC

[jira] [Updated] (ARTEMIS-721) ObjectInputStreamWithClassLoader#resolveClass0 uses Thread.currentThread().getContextClassLoader()

     [ 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)