PDA

View Full Version : jms in spring


b20122034
Sep 10th, 2004, 10:56 AM
hello!
i want to write a simple messaging program by using spring.i am using jms with spring framework.I have made lots of searching about jms in spring on net,read API's and also jms javadocs.But i couldn't connect and got together which i found about spring and jms.Especially ,there are some points which i couldn't understand..(I am using sun application server)

1-I could't create Properties for JndiTamplete..İt is possible to use like this

Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"org.javax.jms.ConnectionFactory");
prop.put(Context.PROVIDER_URL,"tcp://localhost:7676");
JndiTemplate jndi = new JndiTemplate(prop);

2-Must i create a xml file(i don't want to use beans..is it possible?)?

3-SingleConnectionFactory class has a createConnection method and JmsTemplate102 has also..How must i use this method?

and i want to define a simple sender by using jms in spring ?my code is below!could ypu help me to write a sample prodecer in spring?thanks a lot


//import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.QueueConnectionFactory;
import javax.jms.Destination;
import javax.jms.Session;
import javax.jms.MessageProducer;
import javax.jms.MessageConsumer;
import javax.jms.JMSException;

import javax.naming.*;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.JmsTemplate102;
import org.springframework.jndi.JndiTemplate;




import java.util.Properties;

public class JmsMessanger{


ConnectionFactory fabrika;
JmsTemplate jmsTemp;
Queue queue;
JndiTemplate jndi;
Properties prop;
Connection QConnection;
Session oturum;
MessageProducer sender;


public static void main(String args[]){

new JmsMessanger().send();

}

public void send(){

try{

jmsTemp = new JmsTemplate102(fabrika,false);//gives error
prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"org.javax.jms.ConnectionFactory");
prop.put(Context.PROVIDER_URL,"tcp://localhost:7676");

jndi = new JndiTemplate(prop);
fabrika = (QueueConnectionFactory) jndi.lookup("qcf");
queue = (Queue)jndi.lookup("MyQueue");


//jndi.bind("qcf",fabrika);
//jndi.bind("MyQueue",queue);



fabrika = (QueueConnectionFactory)jmsTemp.getConnectionFacto ry();
QConnection = fabrika.createConnection();
QConnection.start();


oturum = QConnection.createSession(false,Session.AUTO_ACKNO WLEDGE);

sender = oturum.createProducer(queue);
jmsTemp.convertAndSend(queue,"hello word");

}catch(NamingException e){

System.err.println("jndi naming exeception hatasi"+e);
System.exit(1);
}catch(JMSException e){

System.err.println("jms exeception hatasi"+e);
System.exit(1);
}

}


public void setConnectionFactory(ConnectionFactory cf) {
fabrika = cf;
}


}

timmorrow
Sep 10th, 2004, 01:31 PM
Have you read chapter 14 of the reference manual? It includes examples on using JmsTemplate102.

In answer to your questions:

1. Are you saying that the code you included for creating JndiTemplate did _not_work? It looks like it should. If not, do you have any specific exceptions or error messages?

2. You don't have to define your beans in an XML file, but of course you end up hardcoding all kinds of dependencies in your code. This is one of the fundamental reasons behind Spring. But it is possible to simply use it as an API.

3. You won't need to use those methods probably (see below).

You might try changing your send() method to something like this:


prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"org.javax.jms.ConnectionFactory");
prop.put(Context.PROVIDER_URL,"tcp://localhost:7676");

jndi = new JndiTemplate(prop);
fabrika = (QueueConnectionFactory) jndi.lookup("qcf");
queue = (Queue)jndi.lookup("MyQueue");

jmsTemp = new JmsTemplate102(fabrika,false);
jmsTemp.convertAndSend(queue,"hello word");


most notably creating the JmsTemplate102 _after_ looking up the connection factory.

Tim

b20122034
Sep 11th, 2004, 11:49 AM
timmorrow thank you for your usefull information..
and i want to ask some questions about jms in spring..

1-prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"org.javax.jms.ConnectionFactory");
prop.put(Context.PROVIDER_URL,"tcp://localhost:7676");

must i have to put other properties in my prop object?
2-what kind of solution must i choose to create a message producer by using spring framework?

factory-->create->connection
connection-->create->session
session-->create->producer

or

jmsTemplate->create producer by using createProducere() method?

3-and when i must use SingleConnectionFactory? must i use this class to create a conenction?

sorry for those lots of questions !! but i am new in spring and i want to learn spring!!thanks for your help!

yybb1978
Sep 13th, 2004, 04:37 AM
hello;

especially i have stuck at a point;

javax.naming.NoInitialContextException: Cannot instantiate class: org.javax.jms.ConnectionFactory [Root exception is java.lang.Clas
sNotFoundException: org.javax.jms.ConnectionFactory]

i used these codes:

prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"org.javax.jms.ConnectionFactory")
prop.put(Context.PROVIDER_URL,"tcp://localhost:7676");
jndi = new JndiTemplate(prop);

i am using Sun Java System Application Server Platform Edition 8.How can i solve this problem?thanks for your help...

timmorrow
Sep 13th, 2004, 07:11 PM
I presume the last two posts are by the same person?

1) With regards JNDI properties: Depends on who is providing your JNDI tree. If you're running inside an application server (e.g. you're doing this in a servlet), you probably won't even need to specify any properties; With an app server you usually define a connection factory as a resource reference in web.xml and map it to an entry in the app server's JNDI tree in the app-server specific web config file. (E.g. jboss-web.xml for JBoss). Then you can get the connection factory by looking it up from "java:comp/env/whatever".

Unfortunately I haven't worked with Sun's app server, so you'll need to consult its documentation of details on how to define the object in JNDI. You might even find that they provide a connection factory already for you, but again you'll need to check the docs for what name it might be bound under.

If you happen to be writing a standalone client, then you will need to specify the environment properties to get at the JNDI server, but again that is server specific. The error you're getting suggests you don't have the name of the initial context factory correct.

You might want to check the forums on java.sun.com for help on this topic.

2) The point of the JmsTemplate is to avoid having to deal too much with factories, connections, sessions, producers etc. I suggest you concentrate on using any of the "send", "convertAndSend" and "execute" methods. I think you'll find "createProducer" etc. are protected methods, so you can't call them anyway.

3) I really don't think you'll need SingleConnectionFactory. JmsTemplate will create your connection using the ConnectionFactory looked-up from JNDI.


Tim

Mark Pollack
Sep 13th, 2004, 10:30 PM
Hi,

Just a small point, you could use the SingleConnectionFactory if you are concerned about the overhead in creating and closing a jms connection for each call to JmsTemplate that sends a message. You should think of this as having the same 'heavyness' as creating and tearing down a jdbc connection. Unlike JDBC, JMS connections can be shared across threads.

To quote from the docs Sec 14.3.1

Spring provides an implementation of the ConnectionFactory interface, SingleConnectionFactory, that will return the same Connection on all createConnection calls and ignore calls to close. This is useful for testing and standalone environments so that the same connection can be used for multiple JmsTemplate calls that may span any number of transactions. SingleConnectionFactory takes a reference to a standard ConnectionFactory that would typically comes from JNDI.


App servers typically provide a connection pool implemenation, much like jdbc connection pools. If you use the SingleConnectionFactory102, remember to indicate the type of messaging 'domain', queues or topics.

Cheers,
Mark

b20122034
Sep 14th, 2004, 09:41 AM
thanks a lot for your help..

now i am getting this exception..I am using sun Application Sever 8.0

javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.jndi.fscontext.RefFSContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.jndi.fscontext.RefFSContextFactory]

my code is:



Properties prop = new Properties();

prop.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.fscontext.RefFSContextFactory");
prop.put(Context.PROVIDER_URL,"iiop://localhost:7676");

JndiTemplate jndi = new JndiTemplate(prop);
QueueConnectionFactory fabrika = (QueueConnectionFactory) jndi.lookup("qcf");

timmorrow
Sep 14th, 2004, 12:30 PM
If you're positive that this the correct intial context factory, then you'll need to include a library in your client's (that is, the code that is performing the JNDI lookup) classpath that contains that class.

The details of specifying the correct environment properties to lookup objects from Sun Application Server 8.0's JNDI tree is really specific to Sun Application Server 8.0 (and isn't limited to getting JMS objects) and certainly is separate from Spring, so I'd really encourage you to look for answers in documentation or forums related to Sun Application Server 8.0.

You might luck out and find someone here who has used Sun Application Server 8.0 to do this... then again maybe not :?

Tim