|
| 1 | +/* |
| 2 | + Copyright (c) 2021, 2022, Oracle and/or its affiliates. |
| 3 | + This software is dual-licensed to you under the Universal Permissive License |
| 4 | + (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License |
| 5 | + 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose |
| 6 | + either license. |
| 7 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + you may not use this file except in compliance with the License. |
| 9 | + You may obtain a copy of the License at |
| 10 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + Unless required by applicable law or agreed to in writing, software |
| 12 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + See the License for the specific language governing permissions and |
| 15 | + limitations under the License. |
| 16 | +*/ |
| 17 | +package rsi.example.amqp; |
| 18 | + |
| 19 | +import oracle.rsi.PushPublisher; |
| 20 | +import oracle.rsi.ReactiveStreamsIngestion; |
| 21 | +import oracle.sql.json.OracleJsonFactory; |
| 22 | +import oracle.sql.json.OracleJsonObject; |
| 23 | +import org.apache.qpid.jms.JmsConnectionFactory; |
| 24 | +import rsi.example.common.Retailer; |
| 25 | +import rsi.example.common.RSIService; |
| 26 | + |
| 27 | +import javax.jms.*; |
| 28 | +import java.io.ByteArrayInputStream; |
| 29 | + |
| 30 | +/** |
| 31 | + * A listener class that listens to inputs from the topic in ActiveMQ using AMQP |
| 32 | + * protocol. RSI service starts at the time when the listener is up. Once the data |
| 33 | + * is received, RSI streams the records into the database. |
| 34 | + */ |
| 35 | +public class Listener { |
| 36 | + private static final String ACTIVEMQ_USER = "admin"; |
| 37 | + private static final String ACTIVEMQ_PASSWORD = "password"; |
| 38 | + private static final String ACTIVEMQ_HOST = "localhost"; |
| 39 | + private static final int ACTIVEMQ_PORT = 5672; |
| 40 | + private static final String TOPIC_NAME = "event"; |
| 41 | + |
| 42 | + // TODO: replace the DB_URL with yours. |
| 43 | + private static final String DB_URL = "jdbc:oracle:thin:@<your-connection-string>"; |
| 44 | + // TODO: replace the DB_USERNAME with your username. |
| 45 | + private static final String DB_USERNAME = "<your-username>"; |
| 46 | + // TODO: replace the DB_PASSWORD with your password. |
| 47 | + private static final String DB_PASSWORD = "<your-password>"; |
| 48 | + |
| 49 | + private static final OracleJsonFactory JSON_FACTORY = new OracleJsonFactory(); |
| 50 | + private static final RSIService RSI_SERVICE = new RSIService(); |
| 51 | + |
| 52 | + public static void main(String[] args) throws Exception { |
| 53 | + // Setup ActiveMQ connection and consumer |
| 54 | + String connectionURI = "amqp://" + ACTIVEMQ_HOST + ":" + ACTIVEMQ_PORT; |
| 55 | + JmsConnectionFactory factory = new JmsConnectionFactory(connectionURI); |
| 56 | + |
| 57 | + Connection connection = factory. |
| 58 | + createConnection(ACTIVEMQ_USER, ACTIVEMQ_PASSWORD); |
| 59 | + connection.start(); |
| 60 | + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| 61 | + |
| 62 | + Destination destination = session. |
| 63 | + createTopic(TOPIC_NAME); |
| 64 | + |
| 65 | + MessageConsumer consumer = session.createConsumer(destination); |
| 66 | + |
| 67 | + long start = System.currentTimeMillis(); |
| 68 | + long count = 1; |
| 69 | + |
| 70 | + // Start up RSI |
| 71 | + RSI_SERVICE.setUrl(DB_URL); |
| 72 | + RSI_SERVICE.setUsername(DB_USERNAME); |
| 73 | + RSI_SERVICE.setPassword(DB_PASSWORD); |
| 74 | + RSI_SERVICE.setSchema(DB_USERNAME); |
| 75 | + RSI_SERVICE.setEntity(Retailer.class); |
| 76 | + ReactiveStreamsIngestion rsi = RSI_SERVICE.start(); |
| 77 | + PushPublisher<Retailer> pushPublisher = ReactiveStreamsIngestion.pushPublisher(); |
| 78 | + pushPublisher.subscribe(rsi.subscriber()); |
| 79 | + |
| 80 | + System.out.println("Waiting for messages..."); |
| 81 | + |
| 82 | + while (true) { |
| 83 | + Message msg = consumer.receive(); |
| 84 | + if (msg instanceof TextMessage) { |
| 85 | + String body = ((TextMessage) msg).getText(); |
| 86 | + |
| 87 | + if (body.trim().equals("SHUTDOWN")) { |
| 88 | + long diff = System.currentTimeMillis() - start; |
| 89 | + System.out.println(String.format("Received %d in %.2f seconds", count, (1.0 * diff / 1000.0))); |
| 90 | + connection.close(); |
| 91 | + |
| 92 | + // close RSI and worker threads |
| 93 | + pushPublisher.close(); |
| 94 | + RSI_SERVICE.stop(); |
| 95 | + |
| 96 | + try { |
| 97 | + Thread.sleep(10); |
| 98 | + } catch (Exception e) { |
| 99 | + } |
| 100 | + System.exit(1); |
| 101 | + |
| 102 | + } else { |
| 103 | + // Create OracleJsonObject from the incoming message |
| 104 | + OracleJsonObject jsonObject = JSON_FACTORY |
| 105 | + .createJsonTextValue( |
| 106 | + new ByteArrayInputStream(body.getBytes())) |
| 107 | + .asJsonObject(); |
| 108 | + |
| 109 | + // Push the data |
| 110 | + pushPublisher.accept(new Retailer(jsonObject)); |
| 111 | + |
| 112 | + if (count == 1) { |
| 113 | + start = System.currentTimeMillis(); |
| 114 | + } else if (count % 1000 == 0) { |
| 115 | + System.out.println(String.format("Received %d messages.", count)); |
| 116 | + } |
| 117 | + count++; |
| 118 | + } |
| 119 | + |
| 120 | + } else { |
| 121 | + System.out.println("Unexpected message type: " + msg.getClass()); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments