我有一个应用程序ONE,它连续读取文件的传入文件夹 一旦有任何文件可用,应用程序就会读取并发布到JMS队列。应用程序TWO正在侦听相同的JMS队列。 这里的消息是同步发布的。所以,当PUBLISHER发布消息时,只等到收到来自RECEIVER的响应。
我面临的问题是RECEIVER多次接收相同的消息。
以下是发布和接收部分的代码段。你能否突出我的错误点。
public class JMSPublisher {
private static final Logger LOGGER = LoggerFactory.getLogger(JMSPublisher.class);
private static final String MYNAME = JMSPublisher.class.getSimpleName();
private BytesMessage bytesMessage = null;
private static final Configuration configuration = Configuration.getInstance();
public synchronized void publishMessage(String destinationName, Manufacturer manufacturer, String fileName, InputStream is, FileDetails fd) throws JMSException {
String queueName = null;
InitialContext initialContext = null;
Object[] params = new Object[]{};
try {
queueName = configuration.getQueueName();
initialContext = getInitialContext(configuration.getProviderUrl());
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) initialContext.lookup(configuration.getConnectionFactory());
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) initialContext.lookup(configuration.getQueueName());
queueConnection.start();
QueueRequestor queueRequestor = new QueueRequestor(queueSession, queue);
bytesMessage = queueSession.createBytesMessage();
// boolean writeSuccess = false;
TextMessage responseMessage = null;
try {
bytesMessage.writeBytes(IOUtils.toByteArray(is));
responseMessage = (TextMessage) queueRequestor.request(bytesMessage);
params = new Object[]{MYNAME, fileName, queueName, bytesMessage.getJMSMessageID()};
LOGGER.info("{}: Published Batch File [{}] to queue [{}] with MessagdID [{}]", params);
params = new Object[]{MYNAME, bytesMessage.getJMSMessageID(), responseMessage.getJMSCorrelationID(), responseMessage.getJMSDestination(), responseMessage.getJMSTimestamp()};
LOGGER.info("{}: Response for Message: MessageID [{}]: JMSCorrelationID [{}] JMSDestination [{}] JMSTimestamp [{}] ", params);
} catch (Exception ex) {
LOGGER.error("{}: Exception occurred while publishing the Batch File [{}] to JMS Queue. Error: [{}]", MYNAME, fileName, ex);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ex) {
LOGGER.error(": Error in closing the InputStream for file [{}] with error [{}]", fileName, ex);
}
}
}
queueConnection.close();
} catch (NamingException ex) {
exceptionOccurred = true;
LOGGER.error("{}: Naming exception occured [{}]", MYNAME, ex);
} finally {
if (initialContext != null) {
try {
initialContext.close();
} catch (NamingException ex) {
LOGGER.error("{}: Naming exception occured while closing initialContext [{}]", MYNAME, ex);
}
}
}
}
private InitialContext getInitialContext(String url) throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, configuration.getInitialFactory());
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
}
=============================================== ================================
public class JmsMessageConsumer implements MessageListener, ExceptionListener {
private QueueConnection queueConnection = null;
private QueueSession queueSession = null;
private InitialContext initialContext = null;
private Configuration cfg = null;
private AbstractApplicationContext ctx = null;
private Configuration config = Configuration.getInstance();
private String majorReturnCode = MajorReturnCode.INTERNAL_ERROR;
private String minorReturnCode = MinorReturnCode.INTERNAL_ERROR;
private QueueConnectionFactory queueConnectionFactory = null;
private Queue queue = null;
private QueueReceiver queueReceiver = null;
public void init() throws NamingException, ConfigException {
cfg = Configuration.getInstance();
initialContext = getInitialContext(cfg.getProviderUrl());
queueConnectionFactory = (QueueConnectionFactory) initialContext.lookup(cfg.getConnectionFactory());
queue = (Queue) initialContext.lookup(cfg.getSimBatchQueue());
getConnection();
}
public void onException(JMSException jmse) {
LOGGER.error("{}.init().onException(): Connection to JMS Queue lost. Error [{}]", MYNAME, jmse.getMessage());
getConnection();
}
public void getConnection() {
try {
queueConnection = queueConnectionFactory.createQueueConnection();
queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queueReceiver = queueSession.createReceiver(queue);
queueReceiver.setMessageListener(this);
queueConnection.setExceptionListener(this);
queueConnection.start();
LOGGER.info("{}.getConnection(): Connected to JMS Queue", MYNAME);
} catch (Exception ex) {
LOGGER.error("{}.getConnection(): Exception occured. Error {{}]", MYNAME, ex);
getConnection();
}
}
@Override
public void onMessage(Message message) {
TextMessage replyMessage = null;
QueueSender replyToSender = null;
try {
BufferedReader br = null;
String simBatchFileName = message.getStringProperty("simBatchFileName");
LOGGER.info("{}: Message received for Batch File [{}]", MYNAME,simBatchFileName );
InputStream jmsMessageConsumer = new BytesMessageInputStream((BytesMessage) message);
byte[] inputBytes = IOUtils.toByteArray(jmsMessageConsumer);
BytesMessage bytesMessage = (BytesMessage) message;
Queue replyTo = (Queue) bytesMessage.getJMSReplyTo();
replyToSender = queueSession.createSender(replyTo);
replyMessage = queueSession.createTextMessage();
setReplyMessage(replyMessage, isSimBatchFileValid, isTimOrderValid, isValidCustomer, timOrderNumber, customerName, validateTimOrder, errorMsg);
} catch (JMSException e) {
LOGGER.error("{}.onMessage(): JMSException occured with error: [{}]", MYNAME, e);
errorMsg = "JMSException occured with error: " + e.getMessage();
try {
if (replyToSender != null) {
replyToSender.send(replyMessage);
}
} catch (JMSException ex) {
LOGGER.error("{}.onMessage(): JMSException occured in re-sending the reply: [{}]", MYNAME, ex);
errorMsg = "JMSException occured with error: " + ex.getMessage();
}
} catch (IOException ex) {
LOGGER.error("{}: Error in reading the message - [{}]", MYNAME, ex);
}
}
private InitialContext getInitialContext(String url) throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, cfg.getInitialFactory());
env.put(Context.PROVIDER_URL, url);
System.setProperty("weblogic.MaxMessageSize", config.getMaxJmsMsgSize());
return new InitialContext(env);
}
private void setReplyMessage(TextMessage replyMessage, boolean isSimBatchFileValid, boolean isTimOrderValid, boolean isValidCustomer, String timOrderNumber, String customerName, boolean validateTimOrder, String errorMsg) throws JMSException {
replyMessage.setBooleanProperty("isSimBatchFileValid", isSimBatchFileValid);
replyMessage.setBooleanProperty("isTimOrderValid", isTimOrderValid);
replyMessage.setBooleanProperty("isValidCustomer", isValidCustomer);
replyMessage.setStringProperty("timOrderNumber", timOrderNumber);
replyMessage.setStringProperty("customerCode", customerName);
replyMessage.setBooleanProperty("validateTimOrder", validateTimOrder);
replyMessage.setStringProperty("simBatchHeaderId", (batchHeaderId != null) ? batchHeaderId.toString() : null);
replyMessage.setStringProperty("majorReturnCode", this.getMajorReturnCode());
replyMessage.setStringProperty("minorReturnCode", this.getMinorReturnCode());
replyMessage.setStringProperty("errorMsg", errorMsg);
}
}
=============================================== ===================
第1行:2017-05-04 12:14:31,590 INFO [ExecuteThread:' 2' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File3_crc_5_1493900070455.csv] 第68行:2017-05-04 12:14:32,917 INFO [ExecuteThread:' 2' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File2_crc_2_1493900070423.csv] 第117行:2017-05-04 12:14:33,665 INFO [ExecuteThread:' 3' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File2_crc_4.csv] 140行:2017-05-04 12:14:33,725 INFO [ExecuteThread:' 2' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File2_crc_2_1493900070423.csv] 第183行:2017-05-04 12:14:33,793 INFO [ExecuteThread:' 2' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File3_crc_5_1493900070455.csv] 205行:2017-05-04 12:14:33,814 INFO [ExecuteThread:' 3' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File1_crc_1_1493900070380.csv] 229行:2017-05-04 12:14:33,846 INFO [ExecuteThread:' 2' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File2_crc_5_1493900070433.csv] 328行:2017-05-04 12:14:34,108 INFO [ExecuteThread:' 0' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File2_crc_5_1493900070433.csv] 第363行:2017-05-04 12:14:34,143 INFO [ExecuteThread:' 2' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File4_crc_2_1493900070465.csv] 399行:2017-05-04 12:14:34,192 INFO [ExecuteThread:' 0' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File1_crc_4.csv] 489行:2017-05-04 12:14:34,435 INFO [ExecuteThread:' 2' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File3_crc_1.csv] 493行:2017-05-04 12:14:34,436 INFO [ExecuteThread:' 3' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File4_crc_1.csv] 523行:2017-05-04 12:14:34,454 INFO [ExecuteThread:' 2' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File1_crc_5.csv] 575行:2017-05-04 12:14:34,499 INFO [ExecuteThread:' 0' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File1_crc_2.csv] 第643行:2017-05-04 12:14:34,812 INFO [ExecuteThread:' 0' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File1_crc_4.csv] 第685行:2017-05-04 12:14:34,866 INFO [ExecuteThread:' 2' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File2_crc_3_1493900070427.csv] 729行:2017-05-04 12:14:34,900 INFO [ExecuteThread:' 3' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File1_crc_5.csv] 753行:2017-05-04 12:14:34,917 INFO [ExecuteThread:' 0' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File4_crc_3_1493900070468.csv] 798行:2017-05-04 12:14:34,953 INFO [ExecuteThread:' 3' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File4_crc_1.csv] 871行:2017-05-04 12:14:35,016 INFO [ExecuteThread:' 2' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File3_crc_2_1493900070440.csv] 911行:2017-05-04 12:14:35,051 INFO [ExecuteThread:' 0' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File1_crc_3_1493900070408.csv] 987行:2017-05-04 12:14:35,268 INFO [ExecuteThread:' 2' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File3_crc_4_1493900070446.csv] 1014行:2017-05-04 12:14:35,823 INFO [ExecuteThread:' 2' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File4_crc_5.csv] 1015行:2017-05-04 12:14:35,823 INFO [ExecuteThread:' 0' for queue:' default'] - JmsMessageConsumer:收到批处理文件的消息[File4_crc_4.csv]