Example usage for javax.jms QueueSession createObjectMessage

List of usage examples for javax.jms QueueSession createObjectMessage

Introduction

In this page you can find the example usage for javax.jms QueueSession createObjectMessage.

Prototype


ObjectMessage createObjectMessage(Serializable object) throws JMSException;

Source Link

Document

Creates an initialized ObjectMessage object.

Usage

From source file:edu.harvard.iq.dvn.core.index.IndexServiceBean.java

private void sendMessage(final IndexEdit op) {
    QueueConnection conn = null;//from  w w w  .  j ava  2 s .  co m
    QueueSession session = null;
    QueueSender sender = null;
    try {
        conn = factory.createQueueConnection();
        session = conn.createQueueSession(false, 0);
        sender = session.createSender(queue);

        Message message = session.createObjectMessage(op);
        sender.send(message);

    } catch (JMSException ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (sender != null) {
                sender.close();
            }
            if (session != null) {
                session.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (JMSException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:edu.harvard.iq.dvn.core.study.StudyFileServiceBean.java

private void addFiles(StudyVersion studyVersion, List<StudyFileEditBean> newFiles, VDCUser user,
        String ingestEmail, int messageLevel) {

    Study study = studyVersion.getStudy();
    MD5Checksum md5Checksum = new MD5Checksum();

    // step 1: divide the files, based on subsettable or not
    List subsettableFiles = new ArrayList();
    List otherFiles = new ArrayList();

    Iterator iter = newFiles.iterator();
    while (iter.hasNext()) {
        StudyFileEditBean fileBean = (StudyFileEditBean) iter.next();
        // Note that for the "special" OtherFiles we want to utilize the 
        // same ingest scheme as for subsettables: they will be queued and 
        // processed asynchronously, and the user will be notified by email.
        // - L.A.
        if (fileBean.getStudyFile().isSubsettable() || fileBean.getStudyFile() instanceof SpecialOtherFile) {
            subsettableFiles.add(fileBean);
        } else {/*from  w w w  .j  ava 2  s  . c o  m*/
            otherFiles.add(fileBean);
            // also add to study, so that it will be flushed for the ids
            fileBean.getStudyFile().setStudy(study);
            study.getStudyFiles().add(fileBean.getStudyFile());

        }
    }

    if (otherFiles.size() > 0) {
        // Only persist the studyVersion we are adding a file that doesn't need to be ingested (non-subsettable)
        if (studyVersion.getId() == null) {
            em.persist(studyVersion);
            em.flush(); // populates studyVersion_id
        } else {
            // There is a problem merging the existing studyVersion,
            // so since all we need from the exisiting version is the versionNote,
            // we get a fresh copy of the object from the database, and update it with the versionNote.
            String versionNote = studyVersion.getVersionNote();
            studyVersion = em.find(StudyVersion.class, studyVersion.getId());
            studyVersion.setVersionNote(versionNote);
        }

    }

    // step 2: iterate through nonsubsettable files, moving from temp to new location
    File newDir = FileUtil.getStudyFileDir(study);
    iter = otherFiles.iterator();
    while (iter.hasNext()) {
        StudyFileEditBean fileBean = (StudyFileEditBean) iter.next();
        StudyFile f = fileBean.getStudyFile();
        File tempFile = new File(fileBean.getTempSystemFileLocation());
        File newLocationFile = new File(newDir, f.getFileSystemName());
        try {
            FileUtil.copyFile(tempFile, newLocationFile);
            tempFile.delete();
            f.setFileSystemLocation(newLocationFile.getAbsolutePath());

            fileBean.getFileMetadata().setStudyVersion(studyVersion);

            em.persist(fileBean.getStudyFile());
            em.persist(fileBean.getFileMetadata());

        } catch (IOException ex) {
            throw new EJBException(ex);
        }
        f.setMd5(md5Checksum.CalculateMD5(f.getFileSystemLocation()));
    }

    // step 3: iterate through subsettable files, sending a message via JMS
    if (subsettableFiles.size() > 0) {
        QueueConnection conn = null;
        QueueSession session = null;
        QueueSender sender = null;
        try {
            conn = factory.createQueueConnection();
            session = conn.createQueueSession(false, 0);
            sender = session.createSender(queue);

            DSBIngestMessage ingestMessage = new DSBIngestMessage(messageLevel);
            ingestMessage.setFileBeans(subsettableFiles);
            ingestMessage.setIngestEmail(ingestEmail);
            ingestMessage.setIngestUserId(user.getId());
            ingestMessage.setStudyId(study.getId());
            ingestMessage.setStudyVersionId(studyVersion.getId());
            ingestMessage.setVersionNote(studyVersion.getVersionNote());

            ingestMessage.setStudyTitle(studyVersion.getMetadata().getTitle());
            ingestMessage.setStudyGlobalId(studyVersion.getStudy().getGlobalId());
            ingestMessage.setStudyVersionNumber(studyVersion.getVersionNumber().toString());
            ingestMessage.setDataverseName(studyVersion.getStudy().getOwner().getName());

            Message message = session.createObjectMessage(ingestMessage);

            String detail = "Ingest processing for " + subsettableFiles.size() + " file(s).";
            studyService.addStudyLock(study.getId(), user.getId(), detail);
            try {
                sender.send(message);
            } catch (Exception ex) {
                // If anything goes wrong, remove the study lock.
                studyService.removeStudyLock(study.getId());
                ex.printStackTrace();
            }

            // send an e-mail
            if (ingestMessage.sendInfoMessage()) {
                mailService.sendIngestRequestedNotification(ingestMessage, subsettableFiles);
            }

        } catch (JMSException ex) {
            ex.printStackTrace();
        } finally {
            try {

                if (sender != null) {
                    sender.close();
                }
                if (session != null) {
                    session.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (JMSException ex) {
                ex.printStackTrace();
            }
        }
    }

    if (!otherFiles.isEmpty()) {
        studyService.saveStudyVersion(studyVersion, user.getId());

    }

}

From source file:edu.harvard.iq.dataverse.ingest.IngestServiceBean.java

public void startIngestJobs(Dataset dataset, AuthenticatedUser user) {
    int count = 0;
    List<DataFile> scheduledFiles = new ArrayList<>();

    IngestMessage ingestMessage = null;/*from   www  .  java2s . c om*/

    for (DataFile dataFile : dataset.getFiles()) {
        if (dataFile.isIngestScheduled()) {
            // todo: investigate why when calling save with the file object
            // gotten from the loop, the roles assignment added at create is removed
            // (switching to refinding via id resolves that)                
            dataFile = fileService.find(dataFile.getId());

            long ingestSizeLimit = -1;
            try {
                ingestSizeLimit = systemConfig.getTabularIngestSizeLimit(
                        getTabDataReaderByMimeType(dataFile.getContentType()).getFormatName());
            } catch (IOException ioex) {
                logger.warning(
                        "IO Exception trying to retrieve the ingestable format identifier from the plugin for type "
                                + dataFile.getContentType() + " (non-fatal);");
            }

            if (ingestSizeLimit == -1 || dataFile.getFilesize() < ingestSizeLimit) {
                dataFile.SetIngestInProgress();
                dataFile = fileService.save(dataFile);

                scheduledFiles.add(dataFile);

                logger.fine("Attempting to queue the file " + dataFile.getFileMetadata().getLabel()
                        + " for ingest, for dataset: " + dataset.getGlobalId());
                count++;
            } else {
                dataFile.setIngestDone();
                dataFile = fileService.save(dataFile);

                logger.info("Skipping tabular ingest of the file " + dataFile.getFileMetadata().getLabel()
                        + ", because of the size limit (set to " + ingestSizeLimit + " bytes).");
                // TODO: (urgent!)
                // send notification to the user!
            }
        }
    }

    if (count > 0) {
        String info = "Attempting to ingest " + count + " tabular data file(s).";
        logger.info(info);
        if (user != null) {
            datasetService.addDatasetLock(dataset.getId(), user.getId(), info);
        } else {
            datasetService.addDatasetLock(dataset.getId(), null, info);
        }

        DataFile[] scheduledFilesArray = (DataFile[]) scheduledFiles.toArray(new DataFile[count]);
        scheduledFiles = null;

        // Sort ingest jobs by file size: 
        Arrays.sort(scheduledFilesArray, new Comparator<DataFile>() {
            @Override
            public int compare(DataFile d1, DataFile d2) {
                long a = d1.getFilesize();
                long b = d2.getFilesize();
                return Long.valueOf(a).compareTo(b);
            }
        });

        ingestMessage = new IngestMessage(IngestMessage.INGEST_MESAGE_LEVEL_INFO);

        for (int i = 0; i < count; i++) {
            ingestMessage.addFileId(scheduledFilesArray[i].getId());
            logger.fine("Sorted order: " + i + " (size=" + scheduledFilesArray[i].getFilesize() + ")");
        }

        QueueConnection conn = null;
        QueueSession session = null;
        QueueSender sender = null;
        try {
            conn = factory.createQueueConnection();
            session = conn.createQueueSession(false, 0);
            sender = session.createSender(queue);

            //ingestMessage.addFile(new File(tempFileLocation));
            Message message = session.createObjectMessage(ingestMessage);

            //try {
            sender.send(message);
            //} catch (JMSException ex) {
            //    ex.printStackTrace();
            //}

        } catch (JMSException ex) {
            ex.printStackTrace();
            //throw new IOException(ex.getMessage());
        } finally {
            try {

                if (sender != null) {
                    sender.close();
                }
                if (session != null) {
                    session.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (JMSException ex) {
                ex.printStackTrace();
            }
        }
    }
}

From source file:org.socraticgrid.taskmanager.TaskManagerImpl.java

/**
 * Queue the message to the task handler.
 *
 * @param msgObject/*from www .  j a va2s  .  co  m*/
 * @return
 */
private QueueResponse queueMessage(java.io.Serializable msgObject) {
    QueueResponse response = new QueueResponse();
    String taskQ = null;
    QueueConnection queueConnection = null;

    try {
        //Get task queue name & queue factory
        taskQ = PropertyAccessor.getProperty(TASKMANAGER_PROPERTY_FILE, PROPERTY_TASK_QUEUE);
        String taskQFactory = PropertyAccessor.getProperty(TASKMANAGER_PROPERTY_FILE,
                PROPERTY_TASK_QUEUE_FACTORY);

        //Get queue connection
        Context jndiContext = new InitialContext();
        QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext
                .lookup(taskQFactory);
        Queue queue = (Queue) jndiContext.lookup(taskQ);

        //Create connection session
        queueConnection = queueConnectionFactory.createQueueConnection();
        QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        QueueSender queueSender = queueSession.createSender(queue);

        //Create message
        ObjectMessage message = queueSession.createObjectMessage(msgObject);

        //Send message
        queueSender.send(message);

        //Set response info
        response.ticket = message.getJMSMessageID();
        response.detail = TASK_MESSAGE_SUCCESS;
    } catch (PropertyAccessException pae) {
        String msg = TASK_MESSAGE_FAILURE + ": error accessing task properties in file:"
                + TASKMANAGER_PROPERTY_FILE + ".";
        log.error(msg, pae);
        response.ticket = TASK_MESSAGE_FAILURE_ID;
        response.detail = msg;
    } catch (NamingException ne) {
        String msg = TASK_MESSAGE_FAILURE + ": error creating connection to queue: " + taskQ + ".";
        log.error(msg, ne);
        response.ticket = TASK_MESSAGE_FAILURE_ID;
        response.detail = msg;
    } catch (JMSException jmse) {
        String msg = TASK_MESSAGE_FAILURE + ": error occurred trying to send notificaiton to task queue: "
                + taskQ + ".";
        log.error(msg, jmse);
        response.ticket = TASK_MESSAGE_FAILURE_ID;
        response.detail = msg;
    } finally {
        //Close queue
        if (queueConnection != null) {
            try {
                queueConnection.close();
            } catch (JMSException e) {
            }
        }
    }

    return response;
}