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:

(2593)

Sending Objects to Restful Services via Post with Marshelling

Hi,

We all know that Restful services works with HTTP protocol. restful services support xml/jamson.

Problem: I have object and I want to send it “as is” to a restful service.

Solutions: 1. not straight forward: We need to convert that object manually to XML string and send it.
2. Straight forward:
2.a Annotate the object with those annotations:
Class annotations:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
Fields annotations:
@XmlElement(name=”fieldName”)

example:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class SomeObject implements Serializable
{
@XmlElement(name=”m”)
int m = 1;
@XmlElement(name=”g”)
int g = 2;
}

Now in order to send it we need to marshell our object using StringWriter and Marshaller.
Example:

Client side:

SomeObject myObject= new SomeObject();
HttpURLConnection conn = null;
StringWriter stringWriter = new StringWriter();

try
{
Marshaller m = JAXBContext.newInstance(SomeObject.class).createMarshaller();
m.marshal(myObject, stringWriter);

URL url = new URL(“http://localhost:8080/MYPROJECT_WEB/hello/testObjects”);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“Content-Type”, “application/xml”);

OutputStream os = conn.getOutputStream();
os.write(stringWriter.toString().getBytes());
os.flush();

BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

String output;
System.out.println(“Output from Server …. n”);
while ((output = br.readLine()) != null)
{
System.out.println(output);
}

}
catch (Exception e)
{

e.printStackTrace();

}
finally
{
conn.disconnect();

}

Server side:

@POST
@Path(“testObjects”)
//@Produces(“application/xml”)
@Consumes(“application/xml”)
// @Consumes(“text/*”)
@Produces(“text/plain”)
public String testObjects(GrandSun sun)
{
System.out.println(sun.toString());
return “Success”;
}

Idan.

Related Articles:

(2593)