这里写自定义目录标题
package com.nstc.dbp.plus;
import javax.jms.Destination;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSslConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import com.icbc.dmqs.des3.Des3;
@Configuration
@PropertySources({ @PropertySource(“classpath:shcpe.properties”), @PropertySource(“classpath:MQReceive.properties”) })
public class MQConfig {
@Value("${RQ_500_NAME}")
private String one;
@Value("${RQ_600_NAME}")
private String two;
@Value("${UseSSL}")
private String UseSSL;
@Value("${AppID}")
private String appID;
@Value("${Password}")
private String password;
@Value("${BrokerUrl}")
private String brokerUrl;
@Value("${ClientID}")
private String clientId;
@Value("${Failover}")
private String Failover;
@Value("${ConnectStrategy}")
private String ConnectStrategy;
@Value("${AckMode}")
private String AckMode;
@Value("${TrustKeyStore}")
private String TrustKeyStore;
@Value("${TrustKeyStorePassword}")
private String TrustKeyStorePassword;
@Autowired
private DbpBean obj;
@Bean
public DefaultMessageListenerContainer listenerContainer() {
DefaultMessageListenerContainer m = new DefaultMessageListenerContainer();
ActiveMQConnectionFactory cf = null;
String connectUrl = getURL();
if (UseSSL.equalsIgnoreCase(“true”)) {
cf = new ActiveMQSslConnectionFactory();
try {
((ActiveMQSslConnectionFactory) cf).setTrustStore(TrustKeyStore);
((ActiveMQSslConnectionFactory) cf).setKeyStorePassword(TrustKeyStorePassword);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
} else {
cf = new ActiveMQConnectionFactory();
}
cf.setBrokerURL(connectUrl);
cf.setUserName(appID);
cf.setPassword(Des3.dec(“ZX71:#AL9sd2=.qx”, password));
cf.setTrustAllPackages(Boolean.TRUE);
if(AckMode.equalsIgnoreCase(“client”)) {
//不设置值,默认自动确认 AUTO_ACKNOWLEDGE
m.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
}
m.setConnectionFactory(cf);
Destination d = new ActiveMQQueue(one + “,” + two);// *表示通配所有队列名称
m.setDestination(d);
m.setClientId(clientId);
m.setMessageListener(new QueueMessageListener(obj));
return m;
}
private String getURL() {
String connectUrl = “”;
if (UseSSL.equalsIgnoreCase(“true”)) {
connectUrl = spritAddressSSL(brokerUrl);
} else {
connectUrl = spritAddress(this.brokerUrl);
}
if (Failover.equalsIgnoreCase(“true”)) {
connectUrl = “failover:(” + connectUrl + “)”;
}
if (ConnectStrategy.isEmpty()) {
connectUrl = connectUrl + “?” + ConnectStrategy;
}
return connectUrl;
}
public static String spritAddressSSL(String serverAddress) {
String[] adds = serverAddress.split(",");
String ret = “”;
for (int i = 0; i < adds.length; ++i) {
ret = ret + “ssl://” + adds[i] + “:21593,”;
}
ret = ret.substring(0, ret.length() - 1);
return ret;
}
public String spritAddress(String serverAddress) {
String[] adds = serverAddress.split(",");
String ret = “”;
String port = “21591”;
for (int i = 0; i < adds.length; ++i) {
ret = ret + “tcp://” + adds[i] + “:” + port + “,”;
}
ret = ret.substring(0, ret.length() - 1);
return ret;
}
}
package com.nstc.dbp.plus;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import org.apache.activemq.command.ActiveMQMessage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.icbc.dmqs.dmqs_client.DMQSMessage;
import com.nstc.dbp.plus.util.RandomGUID;
import com.nstc.dbp.plus.util.SchemaValidator;
import com.nstc.util.TextFormat;
public class QueueMessageListener implements MessageListener {
private static Logger logger = LogManager.getLogger(QueueMessageListener.class);
@Override
public void onMessage(Message arg0) {
ActiveMQMessage msg = (ActiveMQMessage) arg0;
if (msg != null) {
try {
DMQSMessage dmsg = new DMQSMessage((ActiveMQMessage) msg);
if(dmsg.text!=null) {
logger.info(“队列信息【{}】,消息类型【{}】,入列时间【{}】,出列时间【{}】,消息内容【{}】”,msg.getDestination(),dmsg.MessageType,TextFormat.formatDate(new Date(msg.getBrokerInTime()), “yyyy-MM-dd HH:mm:ss SSS”),TextFormat.formatDate(new Date(msg.getBrokerOutTime()), “yyyy-MM-dd HH:mm:ss SSS”),dmsg.text);
String message = dmsg.text;
int index=message.indexOf("{H:");
message = message.substring(index);
//System.out.println(message);
String mid = null;
String OrigSender = null;
String OrigSendDate = null;
String MesgID = null;
try {
OrigSender = message.substring(18, 24);
OrigSendDate = message.substring(30, 38);
MesgID = message.substring(64, 84);
} catch (Exception eee) {
logger.error(“报文头格式错误【{}】”,eee.getMessage());
toErrorFile(RandomGUID.getInatance(), message);
msg.acknowledge();
return;
}
mid = OrigSender + OrigSendDate + MesgID;
String head = message.substring(0, 191);
String msgType = head.substring(44, 55);
String url = getClass().getResource("/schemas/down/" + msgType+".xsd").toString();
if(SchemaValidator.valid(message.substring(191), url, obj.getError())) {
toFile(mid, message);
}
}
msg.acknowledge();
} catch (JMSException | IOException e) {
e.printStackTrace();
}
}
}
private DbpBean obj;
public QueueMessageListener(){}
public QueueMessageListener(DbpBean obj){
this.obj=obj;
}
private void toFile(String messageId, String xmlString) throws IOException {
BufferedWriter bw = null;
try {
String dir=obj.getException();
File directory=new File(dir);
if(!directory.exists()){
directory.mkdirs();
}
File f = new File(dir, messageId + “.xml”);
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), “UTF-8”));
bw.write(xmlString);
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
}
}
}
}
private void toErrorFile(String messageId, String xmlString) throws IOException {
BufferedWriter bw = null;
try {
SimpleDateFormat dtformat = new SimpleDateFormat(“yyyyMMdd”);
String dir = obj.getError() + “/” + dtformat.format(new Date());
File directory=new File(dir);
if(!directory.exists()){
directory.mkdirs();
}
File f = new File(dir, messageId + “.xml”);
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), “UTF-8”));
bw.write(xmlString);
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
}
}
}
}
}
package com.nstc.dbp.plus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(“classpath:DBP.properties”)
public class DbpBean {
@Value("${dbp.error}")
private String error;
@Value("${dbp.exception}")
private String exception;
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public String getException() {
return exception;
}
public void setException(String exception) {
this.exception = exception;
}
}