生产者
package com.web.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.jms.BytesMessage;
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 org.apache.activemq.ActiveMQConnectionFactory;
/**
*
* @author wyk
* @date 2020年8月6日
*
*/
@SuppressWarnings("all")
public class ActiveMqFileUtil {
//默认连接用户名
private static String USERNAME = "admin";
//默认连接密码
private static String PASSWORD = "admin";
//默认连接地址
private static String BROKERURL = "tcp://10.66.10.1:8181";
//文件路径
private static String FILEPATH = "";
//文件名称
private static String FILENAME = "";
//文件类型
private static String FILETYPE = "";
/**
* @param ipAddress 主机IP地址 不能为空
* @param mqUserName 接收端 mq账号 为空状态下 默认账号为admin
* @param mqPwd 接收端mq密码 为空状态下 默认密码为admin
* @param port 接收端mq端口号 为空状态下默认端口号为8181
* @param fileName 要下发的文件名
*/
public ActiveMqFileUtil(String ipAddress,String mqUserName,String mqPwd,String port,String fileName,String fileType) {
if(mqUserName != null && !mqUserName.equals("")) {
USERNAME = mqUserName;
}
if(mqPwd != null && !mqPwd.equals("")) {
PASSWORD = mqPwd;
}
if(port == null || port.equals("")) {
port = "8181";
}
BROKERURL = "tcp://"+ipAddress+":"+port;
try {
String config = "/"+getTaskdirectory()+"etc/config.ini"; //Linux
InputStream ins;
ins = new FileInputStream(config);
Properties prop = new Properties();
prop.load(ins);
if(fileType.equals("策略")) {
FILEPATH = prop.getProperty("strategyPath"); //找到config.ini里面策略管理的路径
}else{
FILEPATH = prop.getProperty("compliancePath"); //找到config.ini里面标准管理的路径
}
FILENAME = fileName;
FILETYPE = fileType;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void send() {
createConnect();
}
public boolean TestConnection() {
//连接工厂
ConnectionFactory connectionFactory;
//连接
Connection connection = null;
//会话,接收或者发送消息的线程
Session session;
//消息的目的地
Destination destination;
//消息生产者
MessageProducer messageProducer;
//实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(ActiveMqFileUtil.USERNAME, ActiveMqFileUtil.PASSWORD, ActiveMqFileUtil.BROKERURL);
try {
//通过连接工厂获取连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
}catch (Exception e) {
return false;
}
return true;
}
public void createConnect() {
//连接工厂
ConnectionFactory connectionFactory;
//连接
Connection connection = null;
//会话,接收或者发送消息的线程
Session session;
//消息的目的地
Destination destination;
//消息生产者
MessageProducer messageProducer;
//实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(ActiveMqFileUtil.USERNAME, ActiveMqFileUtil.PASSWORD, ActiveMqFileUtil.BROKERURL);
try {
//通过连接工厂获取连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//创建session
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
//创建一个名称为Hello World!的消息队列
destination = session.createQueue(FILETYPE + "文件下发");
//创建消息生产者
messageProducer = session.createProducer(destination);
//发送消息
sendMessage(session,messageProducer,FILENAME);
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally{
if(connection != null){
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
/**
* 发送消息
* @param session
* @param messageProducer 消息生产者
* @throws Exception
*/
public static void sendMessage(Session session,MessageProducer messageProducer,String fileName) throws Exception{
InputStream is=null;
File file=new File(FILEPATH + fileName);
// File file=new File("D:\\民生银行总行版\\v-oma-head\\WebContent\\data\\policy\\"+fileName);
BytesMessage bytesMessage=session.createBytesMessage();
is = new FileInputStream(file);
// 读取数据到byte数组中
byte[] buffer=new byte[is.available()];
is.read(buffer);
bytesMessage.writeBytes(buffer);
bytesMessage.setStringProperty("FileName",fileName);
messageProducer.send(bytesMessage);
System.out.println("activeMQ 发送文件成功:"+fileName);
}
public String getTaskdirectory() {
String path=this.getClass().getResource("/").getPath();//得到d:/tomcat/webapps/工程名WEB-INF/classes/路径
path=path.substring(1, path.indexOf("WEB-INF/classes"));//从路径字符串中取出工程路劲
System.out.println("#### "+path);
return path;
}
}
```
消费者
```java
package com.web.util;
import java.io.File;
import java.io.FileOutputStream;
import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JMSConsumer {
//默认连接用户名
private static final String USERNAME = "admin";
//默认连接密码
private static final String PASSWORD = "admin";
//默认连接地址
private static final String BROKERURL = "tcp://10.66.10.1:8181";
public static void main(String[] args) {
//连接工厂
ConnectionFactory connectionFactory;
//连接
Connection connection = null;
//会话,接收或者发送消息的线程
Session session;
//消息目的地
Destination destination;
//消息的消费者
MessageConsumer messageConsumer;
//实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKERURL);
try {
//通过工厂获取连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//创建会话
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建一个连接Hello World!的消息队列
destination = session.createQueue("策略文件下发");
//创建消息的消费者
messageConsumer = session.createConsumer(destination);
while(true){
BytesMessage bytesMessage= (BytesMessage)messageConsumer.receive();
String fileName=bytesMessage.getStringProperty("FileName");
FileOutputStream out=new FileOutputStream("C:\\Users\\yangshan\\Desktop\\"+fileName);
byte[] bytes=new byte[1024];
int len=0;
while ((len=bytesMessage.readBytes(bytes))!=-1){
out.write(bytes,0,len);
}
File file = new File("C:\\Users\\yangshan\\Desktop\\"+fileName);
if(file.exists()) {
System.out.println("本次文件接收完成");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
ActiveMQ工具类代码
于 2021-09-08 17:07:27 首次发布
该代码段展示了如何使用ActiveMQ进行文件的生产和消费。`ActiveMqFileUtil`类用于创建连接并发送文件,而`JMSConsumer`类负责接收并保存文件。生产者通过配置连接信息,读取指定路径的文件内容,将其转化为BytesMessage并通过消息队列发送。消费者监听特定队列,接收到消息后将文件内容写入本地。
摘要由CSDN通过智能技术生成