johnjcasey commented on code in PR #27313:
URL: https://github.com/apache/beam/pull/27313#discussion_r1260077728
##########
sdks/java/io/jms/src/main/java/org/apache/beam/sdk/io/jms/JmsIO.java:
##########
@@ -699,22 +701,24 @@ protected void finalize() {
public abstract static class Write<EventT>
extends PTransform<PCollection<EventT>, WriteJmsResult<EventT>> {
- abstract @Nullable ConnectionFactory getConnectionFactory();
+ public abstract @Nullable ConnectionFactory getConnectionFactory();
Review Comment:
why are these public?
##########
sdks/java/io/jms/src/main/java/org/apache/beam/sdk/io/jms/pool/JmsSessionFactory.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.io.jms.pool;
+
+import static
org.apache.beam.sdk.io.jms.JmsIO.Writer.JMS_IO_PRODUCER_METRIC_NAME;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.JMSException;
+import javax.jms.Session;
+import org.apache.beam.sdk.io.jms.JmsIO;
+import org.apache.beam.sdk.metrics.Counter;
+import org.apache.beam.sdk.metrics.Metrics;
+import org.apache.commons.pool2.BasePooledObjectFactory;
+import org.apache.commons.pool2.PooledObject;
+import org.apache.commons.pool2.impl.DefaultPooledObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@SuppressWarnings({
+ "nullness" // TODO(https://github.com/apache/beam/issues/20497)
+})
+public class JmsSessionFactory<T> extends BasePooledObjectFactory<Session> {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(JmsSessionFactory.class);
+ public static final String CONNECTION_ERRORS_METRIC_NAME =
"connectionErrors";
+
+ private final Counter connectionErrors =
+ Metrics.counter(JMS_IO_PRODUCER_METRIC_NAME,
CONNECTION_ERRORS_METRIC_NAME);
+
+ private final JmsIO.Write<T> spec;
+ private boolean isConnectionClosed;
+
+ public JmsSessionFactory(JmsIO.Write<T> spec) {
+ this.spec = spec;
+ }
+
+ @Override
+ public Session create() throws JMSException {
+ Connection connection;
+ // reset the connection flag
+ this.isConnectionClosed = false;
+ ConnectionFactory connectionFactory = spec.getConnectionFactory();
+ if (spec.getUsername() != null) {
+ connection = connectionFactory.createConnection(spec.getUsername(),
spec.getPassword());
+ } else {
+ connection = connectionFactory.createConnection();
+ }
+ connection.setExceptionListener(
+ exception -> {
+ this.isConnectionClosed = true;
+ this.connectionErrors.inc();
+ });
+ LOG.debug("creating new connection: {}", connection);
+ connection.start();
+ // false means we don't use JMS transaction.
+ return connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ }
+
+ @Override
+ public boolean validateObject(PooledObject<Session> pooledObject) {
+ return !isConnectionClosed && !callSessionMethod(pooledObject.getObject(),
"isClosed", true);
+ }
+
+ @Override
+ public void destroyObject(PooledObject<Session> pooledObject) throws
JMSException {
+ Session session = pooledObject.getObject();
+ session.close();
+ Connection connection = callSessionMethod(session, "getConnection", null);
+ if (connection != null) {
+ connection.close();
+ }
+ }
+
+ @Override
+ public PooledObject<Session> wrap(Session session) {
+ return new DefaultPooledObject<>(session);
+ }
+
+ private <U> U callSessionMethod(Session session, String methodName, U
defaultValue) {
Review Comment:
Why are we doing this via reflection?
##########
sdks/java/io/jms/src/main/java/org/apache/beam/sdk/io/jms/pool/SerializableSessionPool.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.io.jms.pool;
+
+import java.io.Closeable;
+import java.io.Serializable;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.commons.pool2.ObjectPool;
+
+public abstract class SerializableSessionPool<T> implements ObjectPool<T>,
Serializable, Closeable {
Review Comment:
Can we add some tests for this session pool in specific? It seems like there
is a theoretical risk of incorrectly borrowing/returning objects
##########
sdks/java/io/jms/src/main/java/org/apache/beam/sdk/io/jms/pool/JmsSessionFactory.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.io.jms.pool;
+
+import static
org.apache.beam.sdk.io.jms.JmsIO.Writer.JMS_IO_PRODUCER_METRIC_NAME;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.JMSException;
+import javax.jms.Session;
+import org.apache.beam.sdk.io.jms.JmsIO;
+import org.apache.beam.sdk.metrics.Counter;
+import org.apache.beam.sdk.metrics.Metrics;
+import org.apache.commons.pool2.BasePooledObjectFactory;
+import org.apache.commons.pool2.PooledObject;
+import org.apache.commons.pool2.impl.DefaultPooledObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@SuppressWarnings({
Review Comment:
We should not include new blanket nullness supression
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]