Ejb Injection to Spring from Jboss

Hi,

Here is how we lookup from Spring to jboss:

So let’s say we have Interface implemented in jboss:

com.DestBean

The jndi loopup: /DestBean/remote.

in Spring side:

1. Create a bean which trigger the method:

public class TriggerBean
{
	@Autowired
	DestBean destBean;

	public void test()
	{
		boolean result = destBean.setSomething(1, 2);
		System.out.println("the result is="+  result);
	}
}

Now let’s configure applicationContext.xml:




	
		
		java.naming.provider.url">serverhost:port
		java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
		java.naming.factory.url.pkgs=jboss.naming:org.jnp.interfaces
		
	

pay attention that business-interface attribute should have the value of the class-path of your remote interface placed in the target server.

And that’s it!

Idan.

[ad]

Related Articles:

(1585)

Ejb lookup from StandAlone/Tomcat to Jboss5+

Hi,

Lately I was trying to do EJB lookup from my Web Application (Tomcat 6) to my Jboss project(Jboss5.1)

That should be pretty simple but without the right jars it just wont work.

So:

1. Take all jars from the client dir of jboss (Jboss_home_dir/Client/) and put them under the Lib dir of your web project which deployed under tomcat.

2. The client code which does the EJB look up will look like that:


private void ejbCall() throws NamingException
	{

		Properties p = new Properties();
		p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		p.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
		p.put(Context.PROVIDER_URL, "jnp://remote.server.ip:1099");
		InitialContext context = new InitialContext(p);
		String lookupStr = "jndi-remote-lookup-string";
		DispactherBeanRemote dispactherBean = (DispactherBeanRemote) context.lookup(lookupStr);
		MomDispatcherResponseMsgDTO momDispatcherResponseMsgDTO =  dispactherBean.dispatchMsg();
		System.out.println("dispactherBean ="+ momDispatcherResponseMsgDTO.toString());
	}

Again, dont forget to add all the jars from the jboss client. Else you will get lots of weird exceptions,

[ad]

Idan.

Related Articles:

(1585)

Define jboss logs in the application level.

Hi,
The next post will demonstrate how to define jboss logs for a specific application
in a way that it will write all it’s logs to an independent log file.

(tested on Jboss5.1 AS)

Under jboss-dir/conf we have file named: jboss-log4j.xml

1. let’s add new appender:

  
     
     
     
     
     

     
       
     
   

2. let’s add category:

   
     < priority value="debug" />
     < appender-ref ref="projectName" />
   

Now on each class we want to use the Logger(which is imported from org.apache.log4j.Logger) we need to use it this way:

public class Someclass

      private final static Logger logger = Logger.getLogger(Someclass.class);
      public dispatchMsg()
      {
         logger.debug("My first log msg");
      }
}

Idan.

[ad]

Related Articles:

(1585)

Creation and Use of Singleton under Jboss5.1 with EJB3.0 Annotations.

Hi,

I had a case where I needed to use the same variable shared between couple of stateless beans.

In a stand alone java program we would set it as Static.

But since we are under Server application container(this case Jboss) we know that the container has pool of EJB’S sessions which is being created for us.

A static variable in such environment might get duplicated(specially in Cluster mode) and therefor wont serve out target.

So for this purpose we will create Singleton class which will hold our “static” variables.
that Singleton class will have only one instance throughout our application deployment.

Some code:

First we create the Singleton interface:

import javax.ejb.Local;

@Local
public interface SingletonBeanLocal
{
	 int getAttribute();
	 void setAttribute(int x);

}

Now we will create Singleton Management class:

* Note: We must implement Management class in order to achieve Singelton MBean.

import org.jboss.ejb3.annotation.Management;

@Management
public interface SingletonBeanManager
{
	 int getAttribute();
	 void setAttribute(int x);
}

Now we are going to implement these Interfaces:

import javax.ejb.Local;
import org.jboss.ejb3.annotation.Service;

@Service(objectName = "jboss:custom=Singleton")
@Local(
{ SingletonBeanLocal.class })
public class SingletonBean implements SingletonBeanManager, SingletonBeanLocal
{
	private int xyz = 0;

	@Override
	public int getAttribute()
	{
		return xyz;
	}

	@Override
	public void setAttribute(int x)
	{
		xyz++;
	}
}

The @Service and @Management combined together will install a Singleton MBean which can also be controlled via JMX console.

Since this Mbean is registered as service it will be the only one installed and it’s “local” variables” could be use as “Static” in our terms.

Now all we need is to Lookup/Inject to the Singleton Mbean and retrieve/set our variables.

Hope you got it,
Idan.
[ad]

Related Articles:

(1585)

Accessing Jboss JMX programmatically

Hi,

The next example will explain how to retrieve attributes from the JMX console via java.

The reason why you want to access the JMX console would be the need of having values which you wont be able to retrieve via the common API.

For example if we want to get the current subscribers of a specific Topic/Queue resource for any purpose
we wont be able to get it via the common JMS API.

We need to access to the JMX(locally or remotely) and read the attribute we desire.

Some code:

An example of connecting to the JMX and accessing the current number of subscribers which listening to a Topic:


public static int getCurrentTopicSubscribers()
	{

		try
		{
		//use this code when you are connecting remotely(outside the jboss container).
			//------------------------------------
//			Hashtable ht = new Hashtable();
//			ht.put(Context.INITIAL_CONTEXT_FACTORY,
//					"org.jnp.interfaces.NamingContextFactory");
//			ht.put(Context.PROVIDER_URL, "localhost:1099");
//			ht.put(Context.SECURITY_PRINCIPAL, "admin");
//			ht.put(Context.SECURITY_CREDENTIALS, "admin");
//			System.out.println("nt 1- Gotting InitialContext...... ");
//			Context ctx = new InitialContext(ht);
			//---------------------------------

                       //(Connection within the jboss container)
		 MBeanServer server = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);
			server = (MBeanServerConnection) ctx.lookup("jmx/invoker/RMIAdaptor");
			if (server != null)
			{
				monitorJMS();
			}
			else
			{
				log.error("Couldnt connect to JMX server");
			}
		}
		catch (Exception e)
		{
			log.error("Couldnt connect to JMX server");
		}

After we have connection we can easily retrieve the attributes from the JMX:

public static void monitorJMS() throws Exception
	{
		ObjectName objectName = new ObjectName(
				"jboss.messaging.destination:name=DispatcherTopic,service=Topic");
		System.out.println("DurableMessageCount = "
				+ (Integer) server.getAttribute(objectName, new String("DurableMessageCount")));
		System.out.println("AllSubscriptionsCount = "
				+ (Integer) server
						.getAttribute(objectName, new String("AllSubscriptionsCount")));
	}

Idan.
[ad]

Related Articles:

(1585)

Circular dependency bug at Jboss5.1.1 AS

Hi,  
I have been struggeling this for a week now. This is a seriouse bug at Jboss5.1 and EJB3.

Example for Circular dependence:

In CompanyCoreBean

  @EJB(name = "CompanyCoreBean")
  private CompanyCoreBeanLocal companyCoreBean;

CompanyCoreBean is injected to itself

leading to this jboss error:

DEPLOYMENTS MISSING DEPENDENCIES:
Deployment

“jboss.j2ee:ear=WMA_EXPLODED.ear,jar=

WMA_EXPLODEDEJB.jar,name=CompanyCoreBean,service=EJB3” is missing the following dependencies:
Dependency “<UNKNOWN jboss.j2ee:ear=WMA_EXPLODED.ear,jar=

WMA_EXPLODEDEJB.jar,name=CompanyCoreBean,service=EJB3>”

(should be in state “Installed”, but is actually in state “** UNRESOLVED Demands ‘jndi:WMA_EXPLODED/CompanyCoreBean/local-com.mirs.wma.core.sessionBeans.CompanyCoreBeanLocal’ **”)

Dependency “<UNKNOWN

 

There is an open Jira about the issue. but I had to overcome this one.

So the walk around is using the Annotation @IgnoreDependency. which will Ignore it’s own dependency on deployment.

@IgnoreDependency
@EJB(name = "CompanyCoreBean")
private CompanyCoreBeanLocal companyCoreBean;

Idan.

[ad]

Related Articles:

(1585)

Commit data in the middle of Transaction inside the Container

Hi,

Many times I was dealing an issue inside the Application server’s container(Jboss, Weblogic).

I had the need to commit data(Logs, Database) in middle of Transaction.

For example let’s say I want to update some records in the Database and then do some logic on the UPDATED records within the same transaction.

This wont be possible until the current Transaction will finish it’s work.

So the walk through is starting  new Transaction within the current Transaction.

When the container is already performing a transaction, then starting a new transaction will result in the original being suspended. When the new transaction has committed, the original transaction will be resumed.

I am using EJB3.0. The annotation for starting new Transaction within running transaction is

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
protected void doSomeDatabaseUpdate(...)
{
   //Some logic that must be committed before the main transaction finish
}

The result:
The method doSomeDatabaseUpdate will be invoked inside new transaction
and when finished the data will be committed.
Afterwards we can continue with our logic on the updated
data within the main transaction.
Idan.

Related Articles:

(1585)