您好我想发送消息给Active MQ
。我正在使用带有Apache MQ的Spring Boot。我不想使用任何xml类,因此我在Configuration.java
类中定义了bean,并且我想在MessageSenderClass中使用这些bean。我怎么用呢?
我有三个班级:
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 to GE";
System.out.println("MESSAGE-1");
try {
msgSenderClass.onDataLoadMessage(msg);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ("Welcome to 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 is a sample message..sending from 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 for 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>
在AppConfig
文件M中定义bean以及我想在MessageSenderClass中使用的bean如何使用它?