|
| 1 | +/* |
| 2 | + * Copyright 2018 Philipp Salvisberg <philipp.salvisberg@trivadis.com> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.utplsql.sqldev.runner |
| 17 | + |
| 18 | +import java.sql.Connection |
| 19 | +import java.util.List |
| 20 | +import java.util.UUID |
| 21 | +import java.util.logging.Logger |
| 22 | +import oracle.dbtools.raptor.utils.Connections |
| 23 | +import org.utplsql.sqldev.dal.RealtimeReporterDao |
| 24 | +import org.utplsql.sqldev.dal.RealtimeReporterEventConsumer |
| 25 | +import org.utplsql.sqldev.model.runner.RealtimeReporterEvent |
| 26 | + |
| 27 | +class UtPlsqlRunner implements RealtimeReporterEventConsumer { |
| 28 | + |
| 29 | + static val Logger logger = Logger.getLogger(UtPlsqlRunner.name); |
| 30 | + |
| 31 | + var List<String> pathList |
| 32 | + var Connection producerConn |
| 33 | + var Connection consumerConn |
| 34 | + var String reporterId = UUID.randomUUID().toString.replace("-", ""); |
| 35 | + |
| 36 | + new(List<String> pathList, String connectionName) { |
| 37 | + this.pathList = pathList |
| 38 | + setConnection(connectionName) |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * this constructor is intended for tests only |
| 43 | + */ |
| 44 | + new(List<String> pathList, Connection producerConn, Connection consumerConn) { |
| 45 | + this.pathList = pathList |
| 46 | + this.producerConn = producerConn |
| 47 | + this.consumerConn = consumerConn |
| 48 | + } |
| 49 | + |
| 50 | + private def setConnection(String connectionName) { |
| 51 | + if (connectionName === null) { |
| 52 | + throw new RuntimeException("Cannot initialize a RealtimeConsumer without a ConnectionName") |
| 53 | + } else { |
| 54 | + this.producerConn = Connections.instance.cloneConnection(Connections.instance.getConnection(connectionName)) |
| 55 | + this.consumerConn = Connections.instance.cloneConnection(Connections.instance.getConnection(connectionName)) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + def dispose() { |
| 60 | + producerConn.close; |
| 61 | + consumerConn.close; |
| 62 | + } |
| 63 | + |
| 64 | + override void process(RealtimeReporterEvent event) { |
| 65 | + logger.fine(event.toString) |
| 66 | + } |
| 67 | + |
| 68 | + private def void produce() { |
| 69 | + try { |
| 70 | + logger.fine('''Running utPLSQL tests and producing events via reporter id «reporterId»...''') |
| 71 | + val dao = new RealtimeReporterDao(producerConn) |
| 72 | + dao.produceReport(reporterId, pathList) |
| 73 | + logger.fine('''All events produced for reporter id «reporterId».''') |
| 74 | + } catch (Exception e) { |
| 75 | + logger.severe('''Error while producing events for reporter id «reporterId»: «e?.message»''') |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private def void consume() { |
| 80 | + try { |
| 81 | + logger.fine('''Consuming events from reporter id «reporterId» in realtime...''') |
| 82 | + val dao = new RealtimeReporterDao(consumerConn) |
| 83 | + dao.consumeReport(reporterId, this) |
| 84 | + logger.fine('''All events consumed.''') |
| 85 | + } catch (Exception e) { |
| 86 | + logger.severe('''Error while consuming events for reporter id «reporterId»: «e?.message»''') |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + def runAsync() { |
| 91 | + // the producer |
| 92 | + val Runnable producer = [|produce] |
| 93 | + val producerThread = new Thread(producer) |
| 94 | + producerThread.name = "realtime producer" |
| 95 | + producerThread.start |
| 96 | + // the consumer |
| 97 | + val Runnable consumer = [|consume] |
| 98 | + val consumerThread = new Thread(consumer) |
| 99 | + consumerThread.name = "realtime consumer" |
| 100 | + consumerThread.start |
| 101 | + } |
| 102 | +} |
0 commit comments