I have done that. I suspect the problem you are having has to do with how you located your topic/queue's jms factory.
You need to create an appropriate InitialContext to do this lookup. In it
you need to specify the InitialContext.INITIAL_CONTEXT_FACTORY and
InitialContext.PROVIDER_URL (making sure it points to the correct server).
Remember that when you are deploying these applications in separate servers they do not share their JNDI tree.