Example usage for javax.jms MapMessage acknowledge

List of usage examples for javax.jms MapMessage acknowledge

Introduction

In this page you can find the example usage for javax.jms MapMessage acknowledge.

Prototype


void acknowledge() throws JMSException;

Source Link

Document

Acknowledges all consumed messages of the session of this consumed message.

Usage

From source file:org.wso2.carbon.appfactory.eventing.jms.MessageStore.java

public void acknowledgeMessage(String topic, String subscriberId, String messageId) {
    String subscriptionId = Util.getUniqueSubscriptionId(topic, subscriberId);
    Map<String, MapMessage> messages = messageMap.get(subscriptionId);
    if (messages != null) {
        try {/*  w ww. ja v a  2 s  . c om*/
            MapMessage mapMessage = messages.get(messageId);
            if (mapMessage != null) {
                mapMessage.acknowledge();
                messages.remove(messageId);
                if (log.isDebugEnabled()) {
                    log.debug("Message with id:" + messageId + " was acknowledged successfully.");
                }
                return;
            }
        } catch (JMSException e) {
            log.error("Failed to acknowledge message:" + messageId, e);
            // ignore throwing exception as we do not ack the message.
        }
        // we remove the message from map to avoid memory leaks.
        // This message will be delivered back to client from message broker since we do not acknowledge.
        messages.remove(messageId);
    }
}

From source file:org.wso2.carbon.appfactory.resource.mgt.listeners.TenantCreationMessageListener.java

/**
 * @param message - map message which contains data to tenant creation via a rest call.
 *//*from   w  w w  .  j  ava2  s.  c  o  m*/
@Override
public void onMessage(Message message) {

    TenantInfoBean tenantInfoBean = null;
    MapMessage mapMessage;
    if (message instanceof MapMessage) {
        mapMessage = (MapMessage) message;
        String tenantInfoJson;
        try {
            tenantInfoJson = mapMessage.getString(AppFactoryConstants.TENANT_INFO);
            ObjectMapper mapper = new ObjectMapper();
            tenantInfoBean = mapper.readValue(tenantInfoJson, TenantInfoBean.class);

            if (log.isDebugEnabled()) {
                log.debug("Received a message for tenant domain " + tenantInfoBean.getTenantDomain());
            }
            mapMessage.acknowledge();
        } catch (JMSException e) {
            log.error("Error while getting message content.", e);
            throw new RuntimeException(e);
        } catch (JsonParseException e) {
            log.error("Error while converting the json to object.", e);
            throw new RuntimeException(e);
        } catch (JsonMappingException e) {
            log.error("Error while converting the json to object.", e);
            throw new RuntimeException(e);
        } catch (IOException e) {
            log.error("Error while converting the json to object.", e);
            throw new RuntimeException(e);
        }
    }

    try {
        int tenantId = ServiceHolder.getRealmService().getTenantManager()
                .getTenantId(tenantInfoBean.getTenantDomain());
        if (tenantId == MultitenantConstants.INVALID_TENANT_ID) {
            addTenant(tenantInfoBean);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Tenant Already exist, skipping the tenant addition. Tenant domain : "
                        + tenantInfoBean.getTenantDomain() + "and tenant Id : " + tenantInfoBean.getTenantId());
            }
        }

    } catch (JMSException e) {
        String msg = "Can not read received map massage";
        log.error(msg, e);
        throw new RuntimeException(e);
    } catch (AppFactoryException e) {
        String msg = "Can not create tenant";
        log.error(msg, e);
        throw new RuntimeException(e);
    } catch (Exception e) {
        String msg = "Can not create tenant";
        log.error(msg, e);
        throw new RuntimeException(e);
    }
}

From source file:org.wso2.carbon.appfactory.stratos.listeners.StratosSubscriptionMessageListener.java

/**
 * @param message - map message which contains data to stratos subscriptions via a rest call.
 *///  w  ww.ja v a 2s .co m
@Override
public void onMessage(Message message) {
    RuntimeBean[] runtimeBeans;
    TenantInfoBean tenantInfoBean;
    MapMessage mapMessage;
    if (message instanceof MapMessage) {
        mapMessage = (MapMessage) message;
        String tenantInfoJson = null;
        try {
            String runtimesJson = mapMessage.getString(AppFactoryConstants.RUNTIMES_INFO);
            tenantInfoJson = mapMessage.getString(AppFactoryConstants.TENANT_INFO);
            ObjectMapper mapper = new ObjectMapper();
            runtimeBeans = mapper.readValue(runtimesJson, RuntimeBean[].class);
            tenantInfoBean = mapper.readValue(tenantInfoJson, TenantInfoBean.class);
            if (log.isDebugEnabled()) {
                log.debug("Received a message for tenant domain " + tenantInfoBean.getTenantDomain());
            }
            String stage = mapMessage.getString(AppFactoryConstants.STAGE);
            if (!TenantManager.getInstance().tenantExists(tenantInfoBean.getTenantId())) {
                addTenant(tenantInfoBean);
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Tenant Already added in stratos, skipping the tenant addition and continuing "
                            + "with subscription to cartridges. Tenant domain : "
                            + tenantInfoBean.getTenantDomain() + "and tenant Id : "
                            + tenantInfoBean.getTenantId());
                }
            }
            try {
                PrivilegedCarbonContext.startTenantFlow();
                PrivilegedCarbonContext.getThreadLocalCarbonContext()
                        .setTenantDomain(tenantInfoBean.getTenantDomain(), true);
                PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(tenantInfoBean.getAdmin());
                for (RuntimeBean runtimeBean : runtimeBeans) {
                    RepositoryBean repositoryBean = createGitRepository(runtimeBean, tenantInfoBean, stage);
                    subscribe(runtimeBean, tenantInfoBean, repositoryBean, getConfigContext(), stage);
                }
                if (log.isDebugEnabled()) {
                    log.debug("Tenant added and subscribed to cartridges successfully for tenant : "
                            + tenantInfoBean.getTenantDomain());
                }
            } finally {
                PrivilegedCarbonContext.endTenantFlow();
            }
            mapMessage.acknowledge();
            if (log.isDebugEnabled()) {
                log.debug("Message processed successfully. Acknowledged to MB for tenant : "
                        + tenantInfoBean.getTenantDomain());
            }
        } catch (AppFactoryException e) {
            log.error(
                    "Error while subscribing tenant to stratos cartridge. tenantInfoJason : " + tenantInfoJson);
        } catch (JsonMappingException e) {
            log.error(
                    "Error while subscribing tenant to stratos cartridge. tenantInfoJason : " + tenantInfoJson);
        } catch (JsonParseException e) {
            log.error(
                    "Error while subscribing tenant to stratos cartridge. tenantInfoJason : " + tenantInfoJson);
        } catch (IOException e) {
            log.error(
                    "Error while subscribing tenant to stratos cartridge. tenantInfoJason : " + tenantInfoJson);
        } catch (JMSException e) {
            log.error(
                    "Error while subscribing tenant to stratos cartridge. tenantInfoJason : " + tenantInfoJson);
        } catch (Exception e) {
            log.error(
                    "Error while subscribing tenant to stratos cartridge. tenantInfoJason : " + tenantInfoJson);
        }
    } else {
        log.error("Message received is not a Map Message");
    }
}

From source file:org.wso2.carbon.appfactory.stratos.listeners.TenantStratosSubscriptionMessageListener.java

/**
 * @param message - map message which contains data to stratos subscriptions via a rest call.
 *///from  www .j  a v  a 2s  .  c o  m
@Override
public void onMessage(Message message) {
    //TODO remove this log
    log.info("message received to topic name : " + topicSubscriber.toString() + ">>>>>>>>>>>>");

    RuntimeBean[] runtimeBeans = null;
    TenantInfoBean tenantInfoBean = null;

    if (message instanceof MapMessage) {
        try {
            String runtimesJson = ((MapMessage) message).getString(AppFactoryConstants.RUNTIMES_INFO);
            String tenantInfoJson = ((MapMessage) message).getString(AppFactoryConstants.TENANT_INFO);

            ObjectMapper mapper = new ObjectMapper();
            runtimeBeans = mapper.readValue(runtimesJson, RuntimeBean[].class);
            tenantInfoBean = mapper.readValue(tenantInfoJson, TenantInfoBean.class);
            if (log.isDebugEnabled()) {
                log.debug("Received a message for tenant domain " + tenantInfoBean.getTenantDomain());
            }
        } catch (JMSException e) {
            log.error("Error while getting message content.", e);
            throw new RuntimeException(e);
        } catch (JsonParseException e) {
            log.error("Error while converting the json to object.", e);
            throw new RuntimeException(e);
        } catch (JsonMappingException e) {
            log.error("Error while converting the json to object.", e);
            throw new RuntimeException(e);
        } catch (IOException e) {
            log.error("Error while converting the json to object.", e);
            throw new RuntimeException(e);
        }
    }
    MapMessage mapMessage;
    if (message instanceof MapMessage) {
        mapMessage = (MapMessage) message;
        try {
            currentMsgCount++;
            //                //String serverURL = mapMessage.getString(SERVER_URL_FOR_REST);
            //                String tenantAdmin = mapMessage.getString(AppFactoryConstants.TENANT_ADMIN_FOR_REST);
            //                //String tenantAdminPassword = mapMessage.getString(TENANT_ADMIN_PASSWORD_FOR_REST);
            //                String tenantDomain = mapMessage.getString(AppFactoryConstants.TENANT_DOMAIN);
            //               String stage = mapMessage.getString(AppFactoryConstants.STAGE);
            //                String username = tenantAdmin + UserCoreConstants.TENANT_DOMAIN_COMBINER + tenantDomain;
            //               String appendStageToCartridgeInfo = AppFactoryUtil.getAppfactoryConfiguration().
            //                     getFirstProperty(AppFactoryConstants.APPEND_STAGE_TO_CARTRIDGE_INFO);

            String stage = mapMessage.getString(AppFactoryConstants.STAGE);

            addTenant(tenantInfoBean);

            for (RuntimeBean runtimeBean : runtimeBeans) {
                RepositoryBean repositoryBean = createGitRepository(runtimeBean, tenantInfoBean, stage);
                try {
                    PrivilegedCarbonContext.startTenantFlow();
                    PrivilegedCarbonContext.getThreadLocalCarbonContext()
                            .setTenantDomain(tenantInfoBean.getTenantDomain(), true);
                    PrivilegedCarbonContext.getThreadLocalCarbonContext()
                            .setUsername(tenantInfoBean.getAdmin());
                    subscribe(runtimeBean, tenantInfoBean, repositoryBean, getConfigContext(), stage);
                } finally {
                    PrivilegedCarbonContext.endTenantFlow();
                }
            }

            //                restService = new StratosRestService(serverURL, username, tenantAdminPassword);
            //                restService.subscribe(cartridgeType,
            //                                      subscriptionAlias,
            //                                      mapMessage.getString(REPO_URL), true,
            //                                      mapMessage.getString(AppFactoryConstants
            //                                                                 .PAAS_ARTIFACT_STORAGE_REPOSITORY_PROVIDER_ADMIN_USER_NAME),
            //                                      mapMessage.getString(AppFactoryConstants
            //                                                                   .PAAS_ARTIFACT_STORAGE_REPOSITORY_PROVIDER_ADMIN_PASSWORD),
            //                                      mapMessage.getString(DATA_CARTRIDGE_TYPE),
            //                                      mapMessage.getString(DATA_CARTRIDGE_ALIAS),
            //                                      mapMessage.getString(AUTO_SCALE_POLICY),
            //                                      mapMessage.getString(DEPLOYMENT_POLICY));
            //                TODO remove this log
            log.info("subscription done in environment : " + mapMessage.getString(AppFactoryConstants.STAGE)
                    + "of tenant :" + tenantInfoBean.getTenantDomain());
            mapMessage.acknowledge();
        } catch (JMSException e) {
            String msg = "Can not read received map massage at count " + currentMsgCount;
            log.error(msg, e);
            throw new RuntimeException(e);
        } catch (AppFactoryException e) {
            String msg = "Can not subscribe to stratos cartridge";
            log.error(msg, e);
            throw new RuntimeException(e);
        } catch (Exception e) {
            String msg = "Can not subscribe to stratos cartridge";
            log.error(msg, e);
            throw new RuntimeException(e);
        }
    }
}