Java agent ans WAS5

Agent, Error cleaning up agent threads…I have this agent which is calling an EJB.It works fine the first time …when it exits out it gives me an error “Error cleaning up agent threads”.I have checked for active threads before exiting out…

I am using Domino6.02 and WAS5.

import lotus.domino.*;

import hennepin.co.srt.ITGroup;

import hennepin.co.srt.SRTHelper;

import hennepin.co.srt.SRTHelperHome;

import java.rmi.RemoteException;

import java.sql.SQLException;

import java.util.Properties;

import javax.ejb.CreateException;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import com.ibm.websphere.naming.WsnInitialContextFactory;

import com.ibm.websphere.naming.*;

import javax.naming.*;

import java.util.Hashtable;

public class JavaAgent extends AgentBase {

public void NotesMain() {



	try {

		Session session = getSession();

		AgentContext agentContext = session.getAgentContext();

// out("Printing property: " + System.getProperties()); //ty(“java.class.path”));

		testEJB();

	} catch(Exception e) {

		e.printStackTrace();

	}

}



/**

 * Creates connection to testEJB and executes

 * a method on the ejb 

 */

public void testEJB() {

	out("Entered testEJB method");

	 com.ibm.websphere.naming.WsnInitialContextFactory f = new  com.ibm.websphere.naming.WsnInitialContextFactory();

	 out("after  com.ibm.websphere.naming.WsnInitialContextFactory;");

	Properties prop = new Properties();

	prop.put("java.naming.factory.initial", "com.ibm.websphere.naming.WsnInitialContextFactory" );

	prop.put("java.naming.provider.url", "iiop://....");



	Hashtable ht2=new Hashtable();

	ht2.put("java.naming.factory.initial", "com.ibm.websphere.naming.WsnInitialContextFactory" );

	ht2.put("java.naming.provider.url", "iiop://....");



	try {

		 // Get a naming context

		out("before getting context");

		  javax.naming.InitialContext initialContext = new  javax.naming.InitialContext(ht2);

			out("got initial context");			 

		 // Get a reference to Bean

		 Object obj = initialContext.lookup("/lookup....");

         out("looked up SRTHelper");			 

		 SRTHelperHome srtHelperHome = (SRTHelperHome) 

				 javax.rmi.PortableRemoteObject.narrow(obj, SRTHelperHome.class);

			out("got srtHelperHome");

		 // Create an Posting object from the Home interface

		SRTHelper srtHelper = srtHelperHome.create();

			out("got srtHelper");

		 // call the authenticate method

		 ITGroup itGroup = srtHelper.getGroups();

		 out("got group");

		 

		 try {

		 	for (int i = 0; ; i++) {

		 		out("unit number: " + itGroup.getUNIT_NO(i));

		 		out("unit name: " + itGroup.getUNIT_NM(i));

		 	}

		 } catch (Exception e) {

		 }

	} catch (RemoteException e) {

		e.printStackTrace();

	} catch (NamingException e) {

		e.printStackTrace();

	} catch (CreateException e) {

		e.printStackTrace();

	} catch (SQLException e) {

		e.printStackTrace();

	}



}

public void out(String s) {

	System.out.println(s);

}

}

Subject: Java agent ans WAS5

hi,

I’m facing the exactly the same problem.

Very curious to know if you found a solution.

Please post it if you find one.

Thanks.

j.

Subject: RE: Java agent ans WAS5

My last message got truncated during session timeout messages (though why it would save is beyond me :-))

Essentially, what’s happeing is that the new sessions are being created in the same threadgroup as that of the agent’s thread group. When the agent terminates, it tries to clean up all threads associated with the threadgroup, will fail to clean up the thread assoicated with WAS and will throw this error.

I do not know if this leads to memory being leaked out. To be on the safe side, I have a wrapper “class” to handle non-Notes threads. Essentially, the agent will spawn a separate thread (and this is the key) in a separate threadgroup. All your custom processing go into the new thread (which is now running in a different threadgroup). Of course, you will need to determine if all threads are closed out gracefully in case of any error.

That way, when the agent terminates (and the assumption is that you have handled your thread termination gracefully), it will not attempt to clear up the custom threads that now are no longer part of the agent’s thread group.

HTH

Sriram

Subject: RE: Java agent ans WAS5

Sriram, I don’t know where you get your information from (how Threads are used in agents), but this solved my problem.

I’ve been trying to connect to a JBoss 2.4.8 server’s JMS and using the ThreadGroup technique I finally got it working. I don’t know if this is what you meant, but here’s how I did it, for anyone interested:

First off, I am running on a domino 6 client for development. I added a lot of JBoss jar files to the Lotus\Notes6\jvm\lib\ext directory. You need these avaiable.

import lotus.domino.*;

import java.util.Properties;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.jms.Message;

import javax.jms.Queue;

import javax.jms.QueueConnection;

import javax.jms.QueueConnectionFactory;

import javax.jms.QueueSender;

import javax.jms.QueueSession;

public class JavaAgent extends AgentBase {

private class ThreadContents implements Runnable {

	public void run() {

		try {

			String providerUrl = "xxx.xxx.xxx.xxx:1099"; // JBoss server ip address and port

		     String contextFactory = "org.jnp.interfaces.NamingContextFactory";

		     String urlPkgPrefixes = "org.jboss.naming";

		     String queueName = "queue/testQueue";

		        

		     Properties properties = new Properties();

		     properties.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);

		     properties.put(Context.PROVIDER_URL, providerUrl);

		     properties.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);			

					

			InitialContext context = new InitialContext(properties);

					

			System.out.println("lookup session");

		

			QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("QueueConnectionFactory");

		     QueueConnection connection = factory.createQueueConnection();       		

		     QueueSession session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

		        		

		     System.out.println("lookup queue");

		

		     Queue queue = (Queue) context.lookup(queueName);

		        		

		     System.out.println("sending message");

		        

		     QueueSender sender = session.createSender(queue);

		     Message message = session.createTextMessage("this is the message from the domino agent");

		     sender.send(message);

		        		

		     sender.close();

		     connection.close();

		     context.close();

	

			System.out.println("thread done");

		}

		catch(Exception e) {

			e.printStackTrace();

		}

	}

}



public void NotesMain() {

	try {

		Session notessession = getSession();

		AgentContext agentContext = notessession.getAgentContext();

		

		ThreadGroup group = new ThreadGroup("externalgroup");

		

		Thread t = new Thread(group, new ThreadContents());

		t.start();

		t.join();



		if (group != null && !group.isDestroyed()) {

			System.out.println("destroying thread group");

			group.destroy();

		}



		System.out.println("agent done");

	} catch(Exception e) {

		e.printStackTrace();

	}

}

}

Subject: RE: Java agent ans WAS5

I tried your code but modified to connect to WAS but to no avail.After the group.destroy() I get an illegal threadstate exception. the thread that are left have following names :

Thread #2 = LT=0:P=244560:O=0:port=1975

Thread #1 = RT=1:P=244560:O=0:TCPTransportConnection[addr=127.0.0.1,port=1547,local=1974]

Thread #0 = RT=0:P=244560:O=0:TCPTransportConnection[addr=xxx.xxx.xxx.xxx,port=2809,local=1973]

Still amazes me 2 IBM products can’t talk to each other properly…

btw now using webspere runtime of WSAD5.1 and Domino 6.5

j.

Subject: RE: Java agent ans WAS5

btw, leaving the group.destroy() out makes the agents run without errors, but then it won’t run again until I restart the jvm (http task)

j.

Subject: RE: Java agent ans WAS5

I am sorry for the belated response.

ThreadGroup group = new ThreadGroup(“externalgroup”);

Thread t = new Thread(group, new ThreadContents());

You had mentioned the above.

Essentially, that is what I meant by having a new class that wraps this functionality. Essentially, the trick is to start a thread not belonging to the same thread group as that of the agent.

Subject: RE: Java agent ans WAS5

Thanks! I’ll give it a go when i have the time.

j.

Subject: Java agent ans WAS5