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

Auto start jmx #1587

Closed
wants to merge 3 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
2 changes: 1 addition & 1 deletion interlok-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ jar {
"Implementation-Version": project.version,
"Implementation-Vendor-Id": project.group,
"Implementation-Vendor": organizationName,
"Add-Opens": "java.base/java.lang java.base/java.util",
"Add-Opens": "java.base/java.lang java.base/java.util java.management/javax.management",
"Main-Class": "com.adaptris.interlok.boot.InterlokLauncher")
}
from ("$project.buildDir/spring-boot-loader") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.adaptris.core.jms;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;

import com.adaptris.annotation.AdvancedConfig;
import com.adaptris.annotation.DisplayOrder;
import com.adaptris.core.AdaptrisMessage;
import com.thoughtworks.xstream.annotations.XStreamAlias;

import lombok.Getter;
import lombok.Setter;

/**
* <p>
* Translates between <code>AdaptrisMessage</code> and
* <code>javax.jms.TextMessages</code>. Assumes default platform encoding.
* </p>
* <p>
* In the adapter configuration file this class is aliased as
* <b>text-message-translator</b> which is the preferred alternative to the
* fully qualified classname when building your configuration.
* </p>
* <p>
* You must specify a maximum size in bytes that the serialized message may not exceed.
* If it does, then a JMSException is thrown.
* </p>
* <p>
* WARNING: This translator may not work with all JMS vendors due to their messages not being serializable.
* One such case is ActiveMQ.
* </p>
*
* @config text-message-translator
*
*/
@XStreamAlias("size-limited-text-message-translator")
@DisplayOrder(order = { "maxSizeBytes", "metadataFilter", "moveMetadata", "moveJmsHeaders", "reportAllErrors", "limitExceededExceptionMessage" })
public class SizeLimitedTextMessageTranslator extends MessageTypeTranslatorImp{

/**
* This is the maximum size in bytes that the JMS message when serialized may not exceed.
*/
@Min(1)
@NotNull
@Getter
@Setter
private long maxSizeBytes;
/**
* A custom exception message that will be printed to the log file should a message size exceed the maximum allowed.
*/
@AdvancedConfig()
@Getter
@Setter
private String limitExceededExceptionMessage;

private static String DEFAULT_LIMIT_EXCEEDED_EXCEPTION_MESSAGE = "Max message size exceeded.";

public SizeLimitedTextMessageTranslator() {
super();
}

/**
* <p>
* Translates an <code>AdaptrisMessage</code> into a <code>TextMessage</code>
* using the default platform character encoding.
* </p>
*
* @param msg the <code>AdaptrisMessage</code> to translate
* @return a new <code>TextMessage</code>
* @throws JMSException
*/
@Override
public Message translate(AdaptrisMessage msg) throws JMSException {
Message message = helper.moveMetadata(msg, session.createTextMessage(msg.getContent()));

byte[] serializeMessage = serializeMessage(message);
log.trace("JMS message is of size: " + serializeMessage.length);

if(serializeMessage.length > getMaxSizeBytes()) {
throw new JMSException(limitExceededExceptionMessage() + ". " + serializeMessage.length + " bytes");
}

return message;
}

private static byte[] serializeMessage(Message message) throws JMSException {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(message);
oos.flush();
return bos.toByteArray();
} catch (IOException e) {
throw new JMSException("Error serializing message: " + e.getMessage());
}
}

private String limitExceededExceptionMessage() {
return StringUtils.defaultIfBlank(getLimitExceededExceptionMessage(), DEFAULT_LIMIT_EXCEEDED_EXCEPTION_MESSAGE);
}

/**
* <p>
* Translates a <code>TextMessage</code> into an <code>AdaptrisMessage</code>
* using the default platform character encoding.
* </p>
*
* @param msg
* the <code>TextMessage</code> to translate
* @return an <code>AdaptrisMessage</code>
* @throws JMSException
*/
@Override
public AdaptrisMessage translate(Message msg) throws JMSException {
AdaptrisMessage result = currentMessageFactory().newMessage(((TextMessage) msg).getText());
return helper.moveMetadata(msg, result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import javax.management.MalformedObjectNameException;
import javax.management.Notification;
import javax.management.ObjectName;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import com.adaptris.core.Channel;
Expand All @@ -47,6 +49,7 @@ public class ChannelManager extends ComponentManagerImpl<Channel> implements Cha
private transient Set<WorkflowRuntimeManager> workflowManagers;
private transient ObjectName myObjectName = null;
private transient Set<ChildRuntimeInfoComponent> childRuntimeInfoComponents;
private transient String autoStart;

private ChannelManager() {
super();
Expand All @@ -62,6 +65,7 @@ public ChannelManager(Channel c, AdapterManager owner) throws MalformedObjectNam
this();
channel = c;
parent = owner;
autoStart = Boolean.toString(BooleanUtils.toBooleanDefaultIfNull(c.getAutoStart(), true));
initMembers();
if (!skipBackRef) {
parent.addChild(this);
Expand Down Expand Up @@ -240,6 +244,11 @@ public Collection<BaseComponentMBean> getAllDescendants() {
public String getParentId() {
return parent.getUniqueId();
}

@Override
public String getAutoStart() {
return autoStart;
}

@Override
public ObjectName getParentObjectName() throws MalformedObjectNameException {
Expand Down Expand Up @@ -372,4 +381,5 @@ protected String getNotificationType(ComponentNotificationType type) {
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,13 @@ public interface ChannelManagerMBean extends AdapterComponentMBean, ParentRuntim
* @throws MalformedObjectNameException upon ObjectName errors.
*/
boolean removeWorkflow(String id) throws CoreException, IllegalStateException, MalformedObjectNameException;

/**
* Will return the configured auto-start boolean in string form.
* Auto-start determines if this channel should be started when the Adapter starts.
*
* @return "true" if the channel is set to start with the Adapter
*/
String getAutoStart();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.adaptris.core.jms;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import com.adaptris.core.AdaptrisMessage;
import com.adaptris.core.DefaultMessageFactory;
import com.adaptris.core.jms.activemq.EmbeddedActiveMq;

public class SizeLimitedTextMessageTranslatorTest {

public static final String TEXT = "The quick brown fox";

protected static EmbeddedActiveMq activeMqBroker;

@BeforeAll
public static void setUpAll() throws Exception {
activeMqBroker = new EmbeddedActiveMq();
activeMqBroker.start();
}

@AfterAll
public static void tearDownAll() throws Exception {
if(activeMqBroker != null)
activeMqBroker.destroy();
}

@Test
public void defaultTest() {

}

// @Test
// public void testMaxSizeExceeded() throws Exception {
// SizeLimitedTextMessageTranslator trans = new SizeLimitedTextMessageTranslator();
// trans.setMaxSizeBytes(TEXT.getBytes().length - 1);
//
// try {
// AdaptrisMessage aMessage = DefaultMessageFactory.getDefaultInstance().newMessage(TEXT);
// Session session = activeMqBroker.createConnection().createSession(false, Session.CLIENT_ACKNOWLEDGE);
// start(trans, session);
// assertThrows(JMSException.class, () -> trans.translate(aMessage));
//
// } finally {
// stop(trans);
// }
// }
//
// @Test
// public void testMaxSizeExceededCustomMessage() throws Exception {
// SizeLimitedTextMessageTranslator trans = new SizeLimitedTextMessageTranslator();
// trans.setMaxSizeBytes(TEXT.getBytes().length - 1);
// trans.setLimitExceededExceptionMessage("Testing");
//
// try {
// AdaptrisMessage aMessage = DefaultMessageFactory.getDefaultInstance().newMessage(TEXT);
// Session session = activeMqBroker.createConnection().createSession(false, Session.CLIENT_ACKNOWLEDGE);
// start(trans, session);
//
// assertTrue(assertThrows(JMSException.class, () -> trans.translate(aMessage)).getMessage().startsWith("Testing"));
//
// } finally {
// stop(trans);
// }
// }

/**
* @see com.adaptris.core.jms.MessageTypeTranslatorCase#createMessage(javax.jms.Session)
*/

protected Message createMessage(Session session) throws Exception {
return session.createTextMessage(TEXT);
}

/**
* @see com.adaptris.core.jms.MessageTypeTranslatorCase#createTranslator()
*/

protected MessageTypeTranslatorImp createTranslator() throws Exception {
return new SizeLimitedTextMessageTranslator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2266,6 +2266,11 @@ public String getWrappedComponentClassname() {
return Channel.class.getCanonicalName();
}

@Override
public String getAutoStart() {
return "true";
}

}

private class AdapterChild extends StubBaseComponentMBean implements ChildRuntimeInfoComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,37 @@ public void testGetParentId() throws Exception {
adapter.requestClose();
}
}

@Test
public void testGetAutoStart() throws Exception {
String adapterName = this.getClass().getSimpleName() + "." + getName();
Adapter adapter = createAdapter(adapterName);
Channel channel = createChannel(getName(), 0);
channel.setAutoStart(false);
try {
AdapterManager adapterManager = new AdapterManager(adapter);
ChannelManager channelManager = new ChannelManager(channel, adapterManager);
assertFalse(Boolean.parseBoolean(channelManager.getAutoStart()));
}
finally {
adapter.requestClose();
}
}

@Test
public void testGetDefaultAutoStart() throws Exception {
String adapterName = this.getClass().getSimpleName() + "." + getName();
Adapter adapter = createAdapter(adapterName);
Channel channel = createChannel(getName(), 0);
try {
AdapterManager adapterManager = new AdapterManager(adapter);
ChannelManager channelManager = new ChannelManager(channel, adapterManager);
assertTrue(Boolean.parseBoolean(channelManager.getAutoStart()));
}
finally {
adapter.requestClose();
}
}

@Test
public void testProxyEquality() throws Exception {
Expand Down
Loading