Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/artemis rest api #665

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions artemis/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
plugins {
id 'cda.deps-conventions'
id 'cda.java-conventions'
id 'war'
}
dependencies {
//For some reason the caffeine transitive dependency makes gradle angry
implementation(libs.activemq.artemis.server) {
exclude group: "com.github.ben-manes.caffeine", module: "caffeine"
}
implementation(libs.activemq.artemis.rest) {
exclude group: "com.github.ben-manes.caffeine", module: "caffeine"
exclude group: "org.eclipse.jetty.websocket", module: "*"
exclude group: "javax.websocket", module: "*"
exclude group: "org.eclipse.jetty.aggregate", module: "jetty-all"
}
implementation(libs.aqapi)
implementation(libs.jmscommon)
implementation(libs.camel.core)
implementation(libs.camel.jms)
compileOnly(libs.javaee.web.api)
compileOnly(libs.oracle.jdbc.driver)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* MIT License
*
* Copyright (c) 2024 Hydrologic Engineering Center
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package cwms.messaging;

import oracle.jms.AQjmsFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;

import javax.annotation.Resource;
import javax.jms.ConnectionFactory;
import javax.jms.TopicConnectionFactory;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;

public final class CamelServletContextListener implements ServletContextListener {

@Resource(name = "jdbc/CWMS3")
DataSource cwms;
private DefaultCamelContext camelContext;

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {

try {
camelContext = new DefaultCamelContext();
TopicConnectionFactory connectionFactory = AQjmsFactory.getTopicConnectionFactory(new DataSourceWrapper(cwms), true);
camelContext.addComponent("oracleAQ", JmsComponent.jmsComponent(connectionFactory));
ConnectionFactory artemisConnectionFactory = new ActiveMQJMSConnectionFactory("vm://0");
camelContext.addComponent("artemis", JmsComponent.jmsComponent(artemisConnectionFactory));
camelContext.addRoutes(new RouteBuilder() {
public void configure() {
from("oracleAQ:topic:CWMS_20.SWT_TS_STORED?durableSubscriptionName=CDA_SWT_TS_STORED&clientId=CDA")
.log("Received message from CWMS_20.SWT_TS_STORED : ${body}")
//Converting MapMessage to JSON for client processing
//We could have an additional routes for different message formats
.process(new MapMessageToJsonProcessor(camelContext))
.log("Processed message body for Artemis: ${body}")
//Artemis REST API requires the JMS type to be Object
.to("artemis:topic:CDA_SWT_TS_STORED?jmsMessageType=Object");
}
});
camelContext.start();
} catch (Exception e) {
throw new IllegalStateException("Unable to setup Queues", e);
}
}

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
try {
camelContext.stop();
} catch (Exception e) {
throw new IllegalStateException("Unable to stop Camel context during servlet shutdown");
}
}
}
91 changes: 91 additions & 0 deletions artemis/src/main/java/cwms/messaging/DataSourceWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package cwms.messaging;

import oracle.jdbc.driver.OracleConnection;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Logger;


/**
* This class is a wrapper around a DataSource that delegates all calls to the
* wrapped DataSource. It is intended to be extended by classes that need to
* override DataSource methods.
*/
public class DataSourceWrapper implements DataSource {


private DataSource delegate;

/**
* Create a new DelegatingDataSource.
* @param delegate the target DataSource
*/
public DataSourceWrapper(DataSource delegate) {
//wrapped DelegatingDataSource is used because internally AQJMS casts the returned connection
//as an OracleConnection, but the JNDI pool is returning us a proxy, so unwrap it
this.delegate = delegate;
}

/**
* Return the target DataSource that this DataSource should delegate to.
*/

public DataSource getDelegate() {
return this.delegate;
}

@Override
public PrintWriter getLogWriter() throws SQLException {
return getDelegate().getLogWriter();
}

@Override
public void setLogWriter(PrintWriter out) throws SQLException {
getDelegate().setLogWriter(out);
}

@Override
public int getLoginTimeout() throws SQLException {
return getDelegate().getLoginTimeout();
}

@Override
public void setLoginTimeout(int seconds) throws SQLException {
getDelegate().setLoginTimeout(seconds);
}



@Override
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface.isInstance(this)) {
return (T) this;
}
return getDelegate().unwrap(iface);
}

@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return (iface.isInstance(this) || getDelegate().isWrapperFor(iface));
}


@Override
public Logger getParentLogger() {
return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
}

@Override
public Connection getConnection() throws SQLException {
return getDelegate().getConnection().unwrap(OracleConnection.class);
}

@Override
public Connection getConnection(String username, String password) throws SQLException {
return getDelegate().getConnection(username, password).unwrap(OracleConnection.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* MIT License
*
* Copyright (c) 2024 Hydrologic Engineering Center
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package cwms.messaging;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.component.jms.JmsMessage;
import org.apache.camel.impl.DefaultMessage;

import javax.jms.MapMessage;
import javax.jms.TextMessage;
import java.util.Map;

final class MapMessageToJsonProcessor implements Processor {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private final CamelContext context;

MapMessageToJsonProcessor(CamelContext context) {
this.context = context;
}

@SuppressWarnings("unchecked")
@Override
public void process(Exchange exchange) throws Exception {
Message inMessage = exchange.getIn();
//If we use types other than MapMessage or TextMessage, we'd need to handle here
if (((JmsMessage) inMessage).getJmsMessage() instanceof MapMessage) {
Map<String, Object> map = inMessage.getBody(Map.class);
String payload = null;

if (map != null) {
payload = OBJECT_MAPPER.writeValueAsString(map);
}
inMessage.setBody(payload);
inMessage.setHeader(Exchange.CONTENT_TYPE, "application/json");
}
}
}
33 changes: 33 additions & 0 deletions artemis/src/main/resources/broker.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<configuration xmlns="urn:activemq"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">

<core xmlns="urn:activemq:core">

<name>ActiveMQServer</name>

<persistence-enabled>true</persistence-enabled>

<journal-directory>data/journal</journal-directory>
<bindings-directory>data/bindings</bindings-directory>
<large-messages-directory>data/large-messages</large-messages-directory>
<paging-directory>data/paging</paging-directory>

<security-settings>
<!-- Default security configuration -->
</security-settings>

<address-settings>
<!-- Default address configuration -->
<address-setting match="#">
<auto-create-queues>true</auto-create-queues>
<auto-create-addresses>true</auto-create-addresses>
</address-setting>
</address-settings>

<acceptors>
<acceptor name="in-vm">vm://0</acceptor>
</acceptors>
<security-enabled>false</security-enabled>
</core>
</configuration>
35 changes: 35 additions & 0 deletions artemis/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<web-app>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>

<listener>
<listener-class>
org.apache.activemq.artemis.rest.integration.ActiveMQBootstrapListener
</listener-class>
</listener>

<listener>
<listener-class>
org.apache.activemq.artemis.rest.integration.RestMessagingBootstrapListener
</listener-class>
</listener>

<listener>
<listener-class>cwms.messaging.CamelServletContextListener</listener-class>
</listener>

<filter>
<filter-name>Rest-Messaging</filter-name>
<filter-class>
org.jboss.resteasy.plugins.server.servlet.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>Rest-Messaging</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4 changes: 3 additions & 1 deletion cwms-data-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ configurations {
}

configurations.implementation {
exclude group: 'com.oracle.database.jdbc'
exclude group: 'com.oracle.database.jdbc', module: 'ojdbc'
}

dependencies {
Expand Down Expand Up @@ -219,6 +219,8 @@ task generateConfig(type: Copy) {
task run(type: JavaExec) {
group "application"
dependsOn generateConfig

dependsOn(':artemis:war')
dependsOn war

classpath += configurations.baseLibs
Expand Down
Loading
Loading