Skip to content
AsyncTransferService.java 1.43 KiB
Newer Older
Sonia Zorba's avatar
Sonia Zorba committed
/*
 * This file is part of vospace-rest
 * Copyright (C) 2021 Istituto Nazionale di Astrofisica
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package it.inaf.oats.vospace;

import com.fasterxml.jackson.databind.ObjectMapper;
Sonia Zorba's avatar
Sonia Zorba committed
import it.inaf.oats.vospace.exception.InternalFaultException;
import java.io.IOException;
import net.ivoa.xml.uws.v1.JobSummary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
Sonia Zorba's avatar
Sonia Zorba committed
public class AsyncTransferService {
Sonia Zorba's avatar
Sonia Zorba committed
    private static final Logger LOG = LoggerFactory.getLogger(AsyncTransferService.class);

    @Autowired
    private RabbitTemplate template;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    public JobSummary startJob(JobSummary job) {
        try {
            byte[] message = MAPPER.writeValueAsBytes(job);
            byte[] result = (byte[]) template.convertSendAndReceive("start_job_queue", message);

Sonia Zorba's avatar
Sonia Zorba committed
            if (result == null) {
Sonia Zorba's avatar
Sonia Zorba committed
                throw new InternalFaultException("Transfer service returned an empty response");
Sonia Zorba's avatar
Sonia Zorba committed
            }

            LOG.trace("Tape transfer service answered:\n{}", new String(result));

            return MAPPER.readValue(result, JobSummary.class);
        } catch (IOException ex) {
Sonia Zorba's avatar
Sonia Zorba committed
            throw new InternalFaultException(ex);