Commit bbeff598 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Experiment with queue logger

parent f13a9bde
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.api.event;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.StreamHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
/**
*
* @author S. Koulouzis
*/
public class DRIPLogger extends StreamHandler implements AutoCloseable {
private final Connection connection;
private final Channel channel;
private static final String EXCHANGE_NAME = "direct_logs";
public DRIPLogger(String messageBrokerHost) throws IOException, TimeoutException {
super();
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(messageBrokerHost);
factory.setPort(AMQP.PROTOCOL.PORT);
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
}
@Override
public void publish(LogRecord record) {
try {
String level = record.getLevel().getName();
String message = record.getMessage();
channel.basicPublish(EXCHANGE_NAME, level, null, message.getBytes("UTF-8"));
super.publish(record);
} catch (IOException ex) {
Logger.getLogger(DRIPLogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void close() {
if (channel != null && channel.isOpen()) {
try {
channel.close();
} catch (IOException | TimeoutException ex) {
Logger.getLogger(DRIPLogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (connection != null && connection.isOpen()) {
try {
connection.close();
} catch (IOException ex) {
Logger.getLogger(DRIPLogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
...@@ -25,7 +25,10 @@ import java.util.List; ...@@ -25,7 +25,10 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import nl.uva.sne.drip.api.dao.PlanDao; import nl.uva.sne.drip.api.dao.PlanDao;
import nl.uva.sne.drip.api.event.DRIPLogger;
import nl.uva.sne.drip.api.exception.BadRequestException; import nl.uva.sne.drip.api.exception.BadRequestException;
import nl.uva.sne.drip.api.exception.NotFoundException; import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.api.rpc.PlannerCaller; import nl.uva.sne.drip.api.rpc.PlannerCaller;
...@@ -66,12 +69,20 @@ public class PlannerService { ...@@ -66,12 +69,20 @@ public class PlannerService {
@Value("${message.broker.host}") @Value("${message.broker.host}")
private String messageBrokerHost; private String messageBrokerHost;
private final Logger rootLogger;
@Autowired
public PlannerService() throws IOException, TimeoutException {
rootLogger = LogManager.getLogManager().getLogger("");
rootLogger.addHandler(new DRIPLogger(messageBrokerHost));
}
public PlanResponse getPlan(String toscaId) throws JSONException, UnsupportedEncodingException, IOException, TimeoutException, InterruptedException { public PlanResponse getPlan(String toscaId) throws JSONException, UnsupportedEncodingException, IOException, TimeoutException, InterruptedException {
try (PlannerCaller planner = new PlannerCaller(messageBrokerHost)) { try (PlannerCaller planner = new PlannerCaller(messageBrokerHost)) {
Message plannerInvokationMessage = buildPlannerMessage(toscaId); Message plannerInvokationMessage = buildPlannerMessage(toscaId);
rootLogger.info("some message");
Message plannerReturnedMessage = (planner.call(plannerInvokationMessage)); Message plannerReturnedMessage = (planner.call(plannerInvokationMessage));
// Message plannerReturnedMessage = (planner.generateFakeResponse(plannerInvokationMessage));
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
List<MessageParameter> messageParams = plannerReturnedMessage.getParameters(); List<MessageParameter> messageParams = plannerReturnedMessage.getParameters();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment