对于MQ传输文件,我采取的思路是:
1、获取文件流,然后转为byte[ ] 传输文件
2、接受者 用BytesMessage接受byte数据
给有需要的人参考
-------------------------废话少说,看代码-------------------------------
需要的导包 , xml显示依然很捞
org.springframework
spring-jms
javax.jms
javax.jms-api
2.0.1
com.ibm.mq
com.ibm.mq.allclient
9.1.1.0
application.properties 配置文件
#ibm mq 配置信息
project.mq.host= 127.0.0.1
project.mq.port= 1414
#(队列管理器名称)
project.mq.queue-manager= wuliuxietong
#(通道名称)
project.mq.channel= MQ_CHL
#(队列)
project.mq.mqSend= MQ_send
#创建的MQ用户
project.mq.username= mqq
#创建的MQ用户连接密码
project.mq.password= 123456
#连接超时
project.mq.receive-timeout= 20000
jms配置文件
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.connection.JmsTransactionManager;
import org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter;
import org.springframework.jms.core.JmsOperations;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.msg.client.wmq.WMQConstants;
@Configuration
public class JmsConfig {
/**
* 注入连接参数:
* 建立JmsConfig类,添加注解@Configuration,并将以上属性注入到此类
*/
@Value("${project.mq.host}")
private String host;
@Value("${project.mq.port}")
private Integer port;
@Value("${project.mq.queue-manager}")
private String queueManager;
@Value("${project.mq.channel}")
private String channel;
@Value("${project.mq.username}")
private String username;
@Value("${project.mq.password}")
private String password;
@Value("${project.mq.receive-timeout}")
private long receiveTimeout;
/**
* 配置连接工厂:
* CCSID要与连接到的队列管理器一致,Windows下默认为1381,
* Linux下默认为1208。1208表示UTF-8字符集,建议把队列管理器的CCSID改为1208
* @return
*/
@Bean
public MQQueueConnectionFactory mqQueueConnectionFactory() {
MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
mqQueueConnectionFactory.setHostName(host);
try {
mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
mqQueueConnectionFactory.setCCSID(1208);
mqQueueConnectionFactory.setChannel(channel);
mqQueueConnectionFactory.setPort(port);
mqQueueConnectionFactory.setQueueManager(queueManager);
} catch (Exception e) {
e.printStackTrace();
}
return mqQueueConnectionFactory;
}
/**
* 配置连接认证:
* 如不需要账户密码链接可以跳过此步,直接将mqQueueConnectionFactory注入下一步的缓存连接工厂。
* @param mqQueueConnectionFactory
* @return
*/
@Bean
UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter(MQQueueConnectionFactory mqQueueConnectionFactory) {
UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
userCredentialsConnectionFactoryAdapter.setUsername(username);
userCredentialsConnectionFactoryAdapter.setPassword(password);
userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(mqQueueConnectionFactory);
return userCredentialsConnectionFactoryAdapter;
}
/**
* 配置缓存连接工厂:
* 不配置该类则每次与MQ交互都需要重新创建连接,大幅降低速度。
*/
@Bean
@Primary
public CachingConnectionFactory cachingConnectionFactory(UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter) {
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
cachingConnectionFactory.setTargetConnectionFactory(userCredentialsConnectionFactoryAdapter);
cachingConnectionFactory.setSessionCacheSize(500);
cachingConnectionFactory.setReconnectOnException(true);
return cachingConnectionFactory;
}
/**
* 配置事务管理器:
* 不使用事务可以跳过该步骤。
* 如需使用事务,可添加注解@EnableTransactionManagement到程序入口类中,事务的具体用法可参考Spring Trasaction。
* @param cachingConnectionFactory
* @return
*/
@Bean
public PlatformTransactionManager jmsTransactionManager(CachingConnectionFactory cachingConnectionFactory) {
JmsTransactionManager jmsTransactionManager = new JmsTransactionManager();
jmsTransactionManager.setConnectionFactory(cachingConnectionFactory);
return jmsTransactionManager;
}
/**
* 配置JMS模板:
* JmsOperations为JmsTemplate的实现接口。
* 重要:不设置setReceiveTimeout时,当队列为空,从队列中取出消息的方法将会一直挂起直到队列内有消息
* @param cachingConnectionFactory
* @return
*/
@Bean
public JmsOperations jmsOperations(CachingConnectionFactory cachingConnectionFactory) {
JmsTemplate jmsTemplate = new JmsTemplate(cachingConnectionFactory);
jmsTemplate.setReceiveTimeout(receiveTimeout);
return jmsTemplate;
}
}
ibm mq 消息发送者
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsOperations;
import org.springframework.stereotype.Component;
import java.io.*;
/**
* ibm mq 消息发送者
*/
@Component
public class SendMessage {
@Autowired
JmsOperations jmsOperations;
@Value("${project.mq.mqSend}")
private String mqSend;
// 发送消息
public void send(String fileUrl) throws IOException {
// 输入流读取要发送的文件
InputStream in = new FileInputStream(fileUrl);
// 流转为byte数组
byte[] data = new byte[in.available()];
in.read(data);
jmsOperations.convertAndSend(mqSend, data);
in.close();
System.out.println("开始发送消息");
}
}
ibm mq 消息接受者
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsOperations;
import org.springframework.jms.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;
import javax.jms.*;
import java.io.File;
import java.io.FileOutputStream;
/**
* ibm mq 消息接受者
*/
@Component
public class ReceiveMessage extends MessageListenerAdapter {
@Autowired
JmsOperations jmsOperations;
@Override
@JmsListener(destination = "${project.mq.mqSend}") //队列
public void onMessage(Message message) {
try {
//必须转换如果不转换直接message.tostring消息的传输有限制。
//转换成BytesMessage消息
BytesMessage bytesMessage = (BytesMessage) message;
int dataSize = (int) bytesMessage.getBodyLength();
byte[] buffer = new byte[dataSize];
bytesMessage.readBytes(buffer, dataSize);
String path = "E:\\" + "111.xml";
// 调动输出流把文件写到指定的位置
FileOutputStream out = new FileOutputStream(new File(path));
out.write(buffer, 0, buffer.length);
out.close();
System.out.println("MQ_send传来的值为:" );
} catch (Exception e) {
e.printStackTrace();
}
}
}