aspose file tools*
The moose likes Other Application Frameworks and the fly likes Example for JMS sender - receiver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Example for JMS sender - receiver" Watch "Example for JMS sender - receiver" New topic
Author

Example for JMS sender - receiver

Vidhu V S
Greenhorn

Joined: Aug 27, 2008
Posts: 5
Hi,
This is the program I have written for a JMS Sender and receiver using Spring and JBoss Server.



package com.syntel.spring.jms;

import org.springframework.jms.core.*;
import org.springframework.jms.core.MessageCreator;
import javax.jms.*;
public class JmsSender {

private JmsTemplate102 jmsTemplate102;

public JmsTemplate102 getJmsTemplate102() {
return jmsTemplate102;
}

public void setJmsTemplate102(JmsTemplate102 jmsTemplate102) {
this.jmsTemplate102 = jmsTemplate102;
}

public void sendMesage(){
jmsTemplate102.send("JMS_RequestResponseQueue",
new MessageCreator() {
public Message createMessage(Session session)
throws JMSException {
return session.createTextMessage("This is a sample message");
}
});


}
}



package com.syntel.spring.jms;

import org.springframework.jms.core.*;
import javax.jms.*;
public class JmsReceiver {

private JmsTemplate102 jmsTemplate102;

public JmsTemplate102 getJmsTemplate102() {
return jmsTemplate102;
}

public void setJmsTemplate102(JmsTemplate102 jmsTemplate102) {
this.jmsTemplate102 = jmsTemplate102;
}

public void processMessage(){
Message msg = jmsTemplate102.receive("JMS_RequestResponseQueue");
try{
TextMessage textMessage = (TextMessage) msg;
if( msg!=null){
System.out.println(" Message Received -->" +
textMessage.getText());
}


}catch(Exception e){
e.printStackTrace();
}


}
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<bean id="jmsQueueTemplate"
class="org.springframework.jms.core.JmsTemplate102">
<property name="connectionFactory">
<ref bean="jmsQueueConnectionFactory"/>
</property>
<property name="destinationResolver">
<ref bean="jmsDestinationResolver"/>
</property>
<property name="pubSubDomain">
<value>false</value>
</property>
<property name="receiveTimeout">
<value>20000</value>
</property>
</bean>

<bean id="jmsQueueConnectionFactory"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate"/>
</property>
<property name="jndiName">
<value>MQ_JMS_MANAGER</value>
</property>
</bean>

<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
com.sun.jndi.fscontext.RefFSContextFactory
</prop>
<prop key="java.naming.provider.url">
file:/C:/JNDI-Directory
</prop>
</props>
</property>
</bean>

<bean id="jmsSender"
class="com.syntel.spring.jms.JmsSender">
<property name="jmsTemplate102">
<ref bean="jmsQueueTemplate"/>
</property>

</bean>

<bean id="jmsReceiver"
class="com.syntel.spring.jms.JmsReceiver">
<property name="jmsTemplate102">
<ref bean="jmsQueueTemplate"/>
</property>

</bean>



</beans>


package com.syntel.spring.jms;


import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
public class SenderToQueue
{
public static void main(String[] args) {



ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Jms.xml");
JmsSender jmsSender = (JmsSender)context.getBean("jmsSender");

//get the Spring JMSTemplate (here we use the JMS 102 template

}
}


I am getting the following errors

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jmsQueueTemplate' defined in class path resource [Spring-Jms.xml]: Cannot resolve reference to bean 'jmsQueueConnectionFactory' while setting bean property 'connectionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jmsQueueConnectionFactory' defined in class path resource [Spring-Jms.xml]: Invocation of init method failed; nested exception is javax.naming.NotContextException; remaining name 'C:\JNDI-Directory'
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.syntel.spring.jms.SenderToQueue.main(SenderToQueue.java:12)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jmsQueueConnectionFactory' defined in class path resource [Spring-Jms.xml]: Invocation of init method failed; nested exception is javax.naming.NotContextException; remaining name 'C:\JNDI-Directory'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1337)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
... 18 more
Caused by: javax.naming.NotContextException; remaining name 'C:\JNDI-Directory'
at com.sun.jndi.fscontext.FSContext.checkIsDirectory(FSContext.java:885)
at com.sun.jndi.fscontext.FSContext.<init>(FSContext.java:108)
at com.sun.jndi.fscontext.FSContext.<init>(FSContext.java:81)
at com.sun.jndi.fscontext.RefFSContext.<init>(RefFSContext.java:97)
at com.sun.jndi.fscontext.RefFSContextFactory.createContext(RefFSContextFactory.java:42)
at com.sun.jndi.fscontext.RefFSContextFactory.createContextAux(RefFSContextFactory.java:47)
at com.sun.jndi.fscontext.FSContextFactory.getInitialContext(FSContextFactory.java:49)
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at org.springframework.jndi.JndiTemplate.createInitialContext(JndiTemplate.java:137)
at org.springframework.jndi.JndiTemplate.getContext(JndiTemplate.java:104)
at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:86)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:153)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:200)
at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:186)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1368)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1334)
... 28 more


What changes should I make in this

Thanks and regards

Vidhu VS
Syntel Ltd


Vidhu VS
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

"Vidhu V S", you have previously been warned on one or more occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it prior to your next post.

Your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Be aware that accounts with invalid display names are disabled.

bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Example for JMS sender - receiver