java - Send message using JMS and ApacheMQ and spring boot -
hi want send message active mq
. using spring boot apache mq. don't want use xml class have defined beans in configuration.java
class , beans want use in messagesenderclass. how can use it?
i have 3 classes:
1..senderapplication
package com.ge.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.jms.core.jmsmessagingtemplate; @springbootapplication public class senderapplication { /*@autowired jmsmessagesender jmsmessagingsender;*/ public static void main(string[] args) { springapplication.run(senderapplication.class, args); } }
2.. sendingmessagecontroller
package com.ge.controller; import javax.jms.jmsexception; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.autoconfigure.enableautoconfiguration; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.responsebody; import org.springframework.web.bind.annotation.restcontroller; @controller @enableautoconfiguration public class sendingmessagecontroller { @autowired messagesenderclass msgsenderclass; @requestmapping(value="/sending",method= {requestmethod.post}) @responsebody public string greeting() { string msg="welcome ge"; system.out.println("message-1"); try { msgsenderclass.ondataloadmessage(msg); } catch (jmsexception e) { // todo auto-generated catch block e.printstacktrace(); } return ("welcome ge"); } }
3..messagesenderclass
package com.ge.controller; import javax.jms.connection; import javax.jms.connectionfactory; import javax.jms.destination; import javax.jms.jmsexception; import javax.jms.messageproducer; import javax.jms.session; import javax.jms.textmessage; import org.apache.activemq.activemqconnection; import org.apache.activemq.activemqmessageproducer; import org.apache.activemq.activemqsession; import org.apache.activemq.spring.activemqconnectionfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.jms.annotation.jmslistener; import org.springframework.jms.support.destination.destinationresolver; import org.springframework.stereotype.component; import com.ge.config.appconfig; @component public class messagesenderclass { private activemqmessageproducer messageproducer=null; private activemqconnectionfactory activemqconnectionfactory=null; @autowired activemqconnection connection; private activemqsession session=null; private destination destination = null; /*@autowired destinationresolver destinationresolver;*/ @jmslistener(destination = "dataloadchannel") public void ondataloadmessage(string message) throws jmsexception { system.out.println("m in on load data method"); system.out.println(message); connection = (activemqconnection) activemqconnectionfactory.createconnection(); connection.start(); session = (activemqsession) connection.createsession(false, session.auto_acknowledge); destination =session.createqueue("samplequeue"); messageproducer = (activemqmessageproducer) session.createproducer(destination); textmessage message1 = session.createtextmessage(); message1.settext("hello ...this sample message..sending firstclient"); messageproducer.send(message1); system.out.println("sent: " + message1.gettext()); } }
4.applicationconfig
package com.ge.config; import javax.annotation.resource; import javax.jms.connectionfactory; import javax.jms.session; import org.apache.activemq.activemqconnectionfactory; import org.apache.activemq.activemqsession; import org.apache.activemq.redeliverypolicy; import org.apache.activemq.command.activemqqueue; import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; import org.springframework.context.applicationcontext; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.jms.annotation.enablejms; import org.springframework.jms.config.defaultjmslistenercontainerfactory; import org.springframework.jms.support.destination.beanfactorydestinationresolver; import org.springframework.jms.support.destination.destinationresolver; @configuration @enablejms public class appconfig { @resource private applicationcontext applicationcontext; @resource private settings settings; @bean public defaultjmslistenercontainerfactory jmslistenercontainerfactory() { defaultjmslistenercontainerfactory factory = new defaultjmslistenercontainerfactory(); factory.setconnectionfactory(connectionfactory()); factory.setdestinationresolver(destinationresolver()); factory.setconcurrency("9"); factory.settransactionmanager(null); return factory; } @bean public connectionfactory connectionfactory() { activemqconnectionfactory activemqconnectionfactory = new activemqconnectionfactory(settings.getactivemqbrokerurl()); redeliverypolicy policy = new redeliverypolicy(); policy.setmaximumredeliveries(0); activemqconnectionfactory.setredeliverypolicy(policy); return activemqconnectionfactory; } @bean public destinationresolver destinationresolver() { return new beanfactorydestinationresolver(applicationcontext); } @bean public activemqqueue dataloadchannel() { return new activemqqueue(settings.getdataloaddestination()); } }
maven pom.xml:
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>mainjmstemplate</groupid> <artifactid>mainjmstemplate</artifactid> <version>0.0.1-snapshot</version> <properties> <!-- spring version --> <spring-framework.version>4.1.0.release</spring-framework.version> <!-- activemq version --> <activemq.version>5.10.0</activemq.version> </properties> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.3.2.release</version> </parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> <version>1.3.2.release</version> </dependency> <!-- spring aritifacts --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>4.2.0.release</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-thymeleaf</artifactid> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jms</artifactid> <version>4.2.4.release</version> </dependency> <!-- activemq artifacts --> <dependency> <groupid>org.apache.activemq</groupid> <artifactid>activemq-spring</artifactid> <version>${activemq.version}</version> </dependency> </dependencies> <!-- using jdk 1.7 compiling --> <build> <plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <version>2.5.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> <!-- <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.3.2.release</version> </parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> <repositories> <repository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </repository> </repositories> <pluginrepositories> <pluginrepository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </pluginrepository> </pluginrepositories> --> </project>
in appconfig
file m defining beans , beans want use in messagesenderclass how can use it?
Comments
Post a Comment