Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » problem with JMS in RCP
problem with JMS in RCP [message #716116] Tue, 16 August 2011 13:37
mousa.alsulaimi is currently offline mousa.alsulaimiFriend
Messages: 18
Registered: June 2011
Junior Member
hello im trying to create a small RCP application that listens to a Topic in a JMS provider (MQ in glassfish) ... the connection logic works fine outside eclipse RCP .. i mean it works fine in a java SE project ..however when i bring the logic inside the rcp application i get a :
java.lang.NoClassDefFoundError: com/sun/messaging/jms/ra/ManagedConnection
which is caused by :
Caused by: java.lang.ClassNotFoundException: com.sun.messaging.jms.ra.ManagedConnection
in the osgi class loader .

i have had a similar problem when working with EJB and RCP .. but i fixed this problem by changing the class loader .

however this solution did not work for the JMS case.. there some point worth mentioning :
1- the JMS case the look up works fine and returns the correct factory and topic however calling factory.createTopicConnection() will throw the mentioned exception
2- i tried to load "com.sun.messaging.jms.ra.ManagedConnection" using this.getClass.getClassLoader().loadClass("com.sun.messaging.jms.ra.ManagedConnection") and the class got loaded fine , then i tried to load the class using topic.getClass.getClassLoader().loadClass("com.sun.messaging.jms.ra.ManagedConnection")
but an exception was thrown .. topic class loader has the same type as the this object class loader has the deference between topic and this is that topic is initiated thru a look up .

here is my source code :
package froms.views;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.markers.MarkerItem;
import org.eclipse.ui.views.markers.MarkerSupportView;
import org.omg.CORBA.INITIALIZE;

import com.capmtech.financial.account.tx.NewBrokerageAccountTx;
import com.capmtech.financial.workbench.resources.connectivity.ConnectionMgr;
import com.sun.messaging.jms.ra.ManagedConnection;

import froms.fromPages.FormEditorInput;
import froms.multiforms.CustomerFrom;

public class MyMarkerView extends MarkerSupportView  implements ISelectionListener{
	
	
	private IWorkbenchWindow window;

	@Override
	public void init(IViewSite site) throws PartInitException {
		// TODO Auto-generated method stub
		super.init(site);
		this.window =site.getWorkbenchWindow() ; 
	}
	public MyMarkerView() {
		
		super("com.capmtech.financial.forms.markerContentGenerator1");
	    
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().addSelectionListener(this) ; 
		 try{
	    
	    System.out.println("lookin up factorty");
	    TopicConnectionFactory factory = ConnectionMgr.INSTANCE.lookupJMS(TopicConnectionFactory.class, "jms/TestTopic") ;
	    
	    System.out.println("factory aquired ");
	    System.out.println("looking up topic ");
	    Topic topic   = ConnectionMgr.INSTANCE.lookupJMS(Topic.class, "jms/TestTopic1") ; 
	     System.out.println("topic accuired : ");
	     System.out.println(topic.getTopicName() + " this is the topic  " );
	    
	    	System.out.println("lookup is working fine ..... now trying to get the connection ");
	   
//	    	ClassLoader mainLoader = Thread.currentThread().getContextClassLoader() ; 
//		   ClassLoader secondderyLoader = topic.getClass().getClassLoader() ; 
//		  Thread.currentThread().setContextClassLoader(secondderyLoader);
	  String  name= "" ; 
	    	try {
		name ="Thread"  ; 
	    		Thread.currentThread().getContextClassLoader().loadClass("com.sun.messaging.jms.ra.ManagedConnection") ;
		System.out.println("the class is loaded by the thread class loader ");
		name="this" ; 
		getClass().getClassLoader().loadClass("com.sun.messaging.jms.ra.ManagedConnection") ;
		System.out.println("classloader : "+ getClass().getClassLoader().toString() + " threadclassloader 2  : " + Thread.currentThread().getContextClassLoader().toString() + " topic classloader : " + topic.getClass().getClassLoader() );
		System.out.println("this is loaded ");
		name="topic" ; 
		topic.getClass().getClassLoader().loadClass("com.sun.messaging.jms.ra.ManagedConnection") ;
		System.out.println("this is loaded ");
	} catch (ClassNotFoundException e1) {
		// TODO Auto-generated catch block
		System.out.println(" the class was not loaded because of  "+name+" class loader ");
		e1.printStackTrace();
	} 
	     TopicConnection connection2 =factory.createTopicConnection();
	     System.out.println("connection accqired.... now accuireing Session ");
	     TopicSession session2 =  connection2.createTopicSession(false, 0) ;
//	     Thread.currentThread().setContextClassLoader(mainLoader);
	     System.out.println("session accuierd ..... creating topic sub ");
	    //session2.setMessageListener(arg0)
	     TopicSubscriber consumer = session2.createSubscriber(topic) ; 
	    System.out.println("topic sub created ");
	     consumer.setMessageListener(new MessageListener() {
	 		
	 		@Override
	 		public void onMessage(Message arg0) {
	 			// TODO Auto-generated method stub
	 			if(arg0 instanceof TextMessage)
	 				try {
	 					System.out.println(((TextMessage)arg0).getText() + " this is the recived message ");
	 				} catch (JMSException e) {
	 					// TODO Auto-generated catch block
	 					e.printStackTrace();
	 				}
	 		}
	 	}) ;
	     System.out.println("starting connection ");
	    connection2.start() ;
	    System.out.println(" connection started ");
	     }catch(JMSException ex)
	     {
	    	// ex.printStackTrace() ; 
	     }
	    
	    
	
	    
	    
	    
	    
	    
	    
	    
	    
	    
	    
	}

	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		// TODO Auto-generated method stub
	
		if(selection instanceof IStructuredSelection)
		{
			
			IStructuredSelection selectted = (IStructuredSelection) selection   ; 
			if(selectted.getFirstElement() instanceof MarkerItem)
			{
				MarkerItem item1 = (MarkerItem) selectted.getFirstElement() ; 
				
				try {
				if(	item1.getMarker().getAttribute("OBJECT") instanceof NewBrokerageAccountTx)
				{ FormEditorInput input =new FormEditorInput()  ;
				   input.setNewBrokerageAccountTx((NewBrokerageAccountTx)item1.getMarker().getAttribute("OBJECT"));
					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(input, CustomerFrom.ID) ;
				}
				
				
				item1.getMarker().delete() ; 
				} catch (CoreException e) {
					System.out.println("exception  of deleted marker");
					
				} 
			}
		}
	}

}




and this is the look up logic :
 package com.capmtech.financial.workbench.resources.connectivity;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public enum ConnectionMgr {
	INSTANCE;

	private String port;
	private String server;
	private InitialContext context;
	private InitialContext contextJMS;
    
	private ConnectionMgr() {
		server="gf.server";
		port="3700";
	}

	@SuppressWarnings("unchecked")
	public <T> T lookup(final Class<T> remoteInterface, final String jndiName) {
//		instr
		final ClassLoader contextFinder = Thread.currentThread()
				.getContextClassLoader();
		final ClassLoader ejbUtilsClassLoader = getClass().getClassLoader();
		Thread.currentThread().setContextClassLoader(ejbUtilsClassLoader);
		System.out.println(" ejbClassLoader : " + ejbUtilsClassLoader.toString() + " contectFinder : " + contextFinder.toString());
		try {
			return (T) getInitialContext().lookup(jndiName);
		} catch (final NamingException e) {
			throw new RuntimeException("Error looking up " + jndiName, e);
		} finally {
			Thread.currentThread().setContextClassLoader(contextFinder);
		}
	}
	
	public <T> T lookupJMS(final Class<T> remoteInterface, final String jndiName) {
//		instr
		final ClassLoader contextFinder = Thread.currentThread()
				.getContextClassLoader();
		System.out.println("thread class loader " + contextFinder);
		final ClassLoader ejbUtilsClassLoader = getClass().getClassLoader();
	//	Thread.currentThread().setContextClassLoader(ejbUtilsClassLoader);
		try {
			return (T) getInitialContextJMS().lookup(jndiName);
		} catch (final NamingException e) {
			throw new RuntimeException("Error looking up " + jndiName, e);
		} finally {
			Thread.currentThread().setContextClassLoader(contextFinder);
		}
	}

	private Context getInitialContext() {
		if (context != null)
			return context;
		return createInitialContextGF();
	}
	
	private Context getInitialContextJMS() {
		if (contextJMS != null)
			return contextJMS;
		return createInitialContextJMS();
	}

	private Context createInitialContextGF() {
		try {
			context = new InitialContext(createPropertiesExistingGF());
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return context;
	}

	private Context createInitialContextJMS()
	{
		try{
	         contextJMS=	new InitialContext(createPropertiesJMS());	
	         
		}catch(NamingException ex)
		{
			ex.printStackTrace() ; 
		}
		
		return contextJMS ;
	}
	private Properties createPropertiesExistingGF() {
		final Properties props = new Properties();
		props.setProperty(InitialContext.STATE_FACTORIES,
				"com.sun.enterprise.naming.SerialInitContextFactory");
		props.setProperty("java.naming.factory.url.pkgs",
				"com.sun.enterprise.naming");
		props.setProperty("java.naming.factory.state",
				"com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
		if (!server.isEmpty())
			props.setProperty("org.omg.CORBA.ORBInitialHost", server);
		if (!port.isEmpty())
			props.setProperty("org.omg.CORBA.ORBInitialPort", port);
//		props.setProperty(context.PROVIDER_URL, "iiop:)
		props.setProperty(Context.PROVIDER_URL,"iiop://gf.server:3700");
		// props.setProperty("com.sun.appserv.iiop.orbconnections","5");
		// Increase ORB Response Timeout to 5 min instead of 30 min:
		// props.setProperty("com.sun.corba.ee.transport.ORBTCPTimeouts",
		// "500:90000:20");
		props.setProperty(
				"com.sun.corba.ee.transport.ORBWaitForResponseTimeout",
				"300000");
		return props;
	}
	
	private Properties createPropertiesJMS()
	{
		final Properties props = new Properties() ; 
		
		   props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
	        props.put(Context.PROVIDER_URL, "iiop://localhost:3700");
		
		
		return props  ; 
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getPort() {
		return port;
	}

	public void setServer(String server) {
		this.server = server;
	}

	public String getServer() {
		return server;
	}
}




Previous Topic: How to log warnings and infos in eclipse 3.7 ?
Next Topic:Workbench seems to be full of multi-user issues.
Goto Forum:
  


Current Time: Thu Apr 25 17:36:40 GMT 2024

Powered by FUDForum. Page generated in 0.02735 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top