/**
 * OpenCPS is the open source Core Public Services software
 * Copyright (C) 2016-present OpenCPS community
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>
 */

package org.opencps.backend.exc;

import javax.jms.JMSException;
import javax.naming.NamingException;

import org.opencps.jms.context.JMSContext;
import org.opencps.jms.util.JMSMessageBodyUtil;
import org.opencps.jms.util.JMSMessageUtil;
import org.opencps.util.PortletUtil;
import org.opencps.util.WebKeys;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.messaging.Message;
import com.liferay.portal.kernel.messaging.MessageListener;
import com.liferay.portal.kernel.messaging.MessageListenerException;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.util.PortalUtil;

/**
 * @author khoavd
 */
public class MsgInFrontOffice implements MessageListener {

	/*
	 * (non-Javadoc)
	 * @see
	 * com.liferay.portal.kernel.messaging.MessageListener#receive(com.liferay
	 * .portal.kernel.messaging.Message)
	 */
	@Override
	public void receive(Message message)
		throws MessageListenerException {

		_doReceive(message);
	}

	private void _doReceive(Message message) {

		System.out.println("MsgInFrontOffice/////////////////////////////////");

		long[] companyIds = PortalUtil.getCompanyIds();

		long companyId = 0;

		if (companyIds != null && companyIds.length > 0) {
			for (int i = 0; i < companyIds.length; i++) {
				if (PortletUtil.checkJMSConfig(companyIds[i])) {
					companyId = companyIds[i];
					break;
				}
			}
		}

		if (companyId > 0) {
			JMSContext context =
				JMSMessageUtil.createConsumer(
					companyId, StringPool.BLANK, true,
					WebKeys.JMS_QUEUE_OPENCPS_FRONTOFFICE.toLowerCase(),
					"local");
			try {

				int receiveNumber = 10;

				int count = 1;

				while (count <= receiveNumber) {
					_log.info("Start receive message/////////////////////////////////////");
					javax.jms.Message jsmMessage =
						context.getMessageConsumer().receive(3000);
					_log.info("Received message/////////////////////////////////////");
					if (jsmMessage != null) {
						JMSMessageBodyUtil.receiveMessage(context, jsmMessage);
					}
					else {
					}

					count++;
				}
			}
			catch (Exception e) {
				_log.error(e);
			}
			finally {
				try {
					context.destroy();
					_log.info("Destroy Received message/////////////////////////////////////");
				}
				catch (JMSException e) {
					_log.error(e);
				}
				catch (NamingException e) {
					_log.error(e);
				}
			}

		}
		else {
			_log.info("Cannot create connection to JMS Queue..................");
		}

	}
	private Log _log = LogFactoryUtil.getLog(MsgInFrontOffice.class);
}

@@ -2426,17 +2426,17 @@
 umber = 
-1
+5
 0;%0A%0A%09%09%09%09
@@ -2489,24 +2489,26 @@
 ber) %7B%0A%09%09%09%09%09
+//
 _log.info(%22S
@@ -2647,17 +2647,17 @@
 receive(
-3
+1
 000);%0A%09%09
@@ -2651,32 +2651,34 @@
 ive(1000);%0A%09%09%09%09%09
+//
 _log.info(%22Recei

change waiting time to receving message