Last active
May 18, 2018 21:02
-
-
Save gliviu/9320a2f426066228489f1a59b2e71653 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package activemq.publisher; | |
| import java.io.File; | |
| import java.util.Arrays; | |
| import java.util.stream.Collectors; | |
| import javax.jms.ConnectionFactory; | |
| import org.apache.activemq.ActiveMQConnectionFactory; | |
| import org.apache.camel.CamelContext; | |
| import org.apache.camel.ProducerTemplate; | |
| import org.apache.camel.builder.RouteBuilder; | |
| import org.apache.camel.component.jms.JmsComponent; | |
| import org.apache.camel.impl.DefaultCamelContext; | |
| import org.apache.commons.io.FileUtils; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| /** | |
| * Synchronous jms message service. | |
| * | |
| * <dependency> | |
| <groupId>org.apache.camel</groupId> | |
| <artifactId>camel-core</artifactId> | |
| <version>${camel.version}</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.camel</groupId> | |
| <artifactId>camel-jms</artifactId> | |
| <version>${camel.version}</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.activemq</groupId> | |
| <artifactId>activemq-camel</artifactId> | |
| <version>${activemq.version}</version> | |
| </dependency> | |
| <!-- Activemq dependencies --> | |
| <dependency> | |
| <groupId>org.apache.activemq</groupId> | |
| <artifactId>activemq-pool</artifactId> | |
| <version>${activemq.version}</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>commons-io</groupId> | |
| <artifactId>commons-io</artifactId> | |
| <version>2.4</version> | |
| </dependency> | |
| */ | |
| public class Publisher { | |
| static Logger LOG = LoggerFactory.getLogger(Publisher.class); | |
| public static void main(String[] args) throws Exception { | |
| if(args.length!=1){ | |
| System.out.printf("Wrong arguments\n"); | |
| } | |
| String fileContent = FileUtils.readFileToString(new File(args[0])); | |
| final CamelContext context = new DefaultCamelContext(); | |
| ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("karaf", "karaf", "tcp://localhost:61616"); | |
| context.addComponent("test-jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory)); | |
| String[] lines = fileContent.split(String.format("%n")); | |
| final String targetName = lines[0]; | |
| String message = Arrays.stream(lines, 1, lines.length).collect(Collectors.joining(String.format("%n"))); | |
| final boolean isTopic = targetName.contains("VirtualTopic"); | |
| context.addRoutes(new RouteBuilder() { | |
| @Override | |
| public void configure() throws Exception { | |
| if(isTopic){ | |
| from("direct:publisher").to("test-jms:topic:"+targetName); | |
| } else{ | |
| from("direct:publisher").to("test-jms:queue:"+targetName); | |
| } | |
| } | |
| }); | |
| context.start(); | |
| ProducerTemplate producerTemplate = context.createProducerTemplate(); | |
| producerTemplate.sendBody("direct:publisher", message); | |
| context.stop(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment