/java/

Wildfly 8.1.0.Final Hornetq JMS.

2016-07-24 17:13:12

GIT

Xml Configuration never end.

Jboss configuration

What I mean by that. You have to start your Jboss with standalone-full.xml configuration, and adjust jms-destinations by


<jms-queue name='"TestQueue"'>
<entry name='"java:jboss/jms/queue/testQueue"/'>
<durable>false</durable>
</entry></jms-queue>

or you can take standalone.xml but then, there is more steps.

Step 1 - extension


<extensions>
    ...
    <extension module='"org.jboss.as.messaging"/'>
    ...
</extension></extensions>

Step 2 - subsystem xmlns="urn:jboss:domain:ejb3:2.0"


<subsystem xmlns='"urn:jboss:domain:ejb3:2.0"'>
    ...
    <mdb>
<resource-adapter-ref resource-adapter-name='"${ejb.resource-adapter-name:hornetq-ra.rar}"/'>
<bean-instance-pool-ref pool-name='"mdb-strict-max-pool"/'>
</bean-instance-pool-ref></resource-adapter-ref></mdb>
    ...
</subsystem>

Step 3 - subsystem xmlns="urn:jboss:domain:messaging:2.0" FOR hornetq-server


<subsystem xmlns='"urn:jboss:domain:messaging:2.0"'>
<hornetq-server>
<journal-file-size>102400</journal-file-size>
<connectors>
<http-connector name='"http-connector"' socket-binding='"http"'>
<param key='"http-upgrade-endpoint"' value='"http-acceptor"/'/>
</http-connector>
<http-connector name='"http-connector-throughput"' socket-binding='"http"'>
<param key='"http-upgrade-endpoint"' value='"http-acceptor-throughput"/'/>
<param key='"batch-delay"' value='"50"/'/>
</http-connector>
<in-vm-connector name='"in-vm"' server-id='"0"/'>
</in-vm-connector></connectors>
<acceptors>
<http-acceptor http-listener='"default"/' name='"http-acceptor"'>
<http-acceptor http-listener='"default"' name='"http-acceptor-throughput"'>
<param key='"batch-delay"' value='"50"/'/>
<param key='"direct-deliver"' value='"false"/'/>
</http-acceptor>
<in-vm-acceptor name='"in-vm"' server-id='"0"/'>
</in-vm-acceptor></http-acceptor></acceptors>
<security-settings>
<security-setting match='"#"'>
<permission roles='"guest"/' type='"send"'>
<permission roles='"guest"/' type='"consume"'>
<permission roles='"guest"/' type='"createNonDurableQueue"'>
<permission roles='"guest"/' type='"deleteNonDurableQueue"'>
</permission></permission></permission></permission></security-setting>
</security-settings>
<address-settings>
<!--default for catch all-->
<address-setting match='"#"'>
<dead-letter-address>jms.queue.DLQ</dead-letter-address>
<expiry-address>jms.queue.ExpiryQueue</expiry-address>
<max-size-bytes>10485760</max-size-bytes>
<page-size-bytes>2097152</page-size-bytes>
<message-counter-history-day-limit>10</message-counter-history-day-limit>
</address-setting>
</address-settings>
<jms-connection-factories>
<connection-factory name='"InVmConnectionFactory"'>
<connectors>
<connector-ref connector-name='"in-vm"/'>
</connector-ref></connectors>
<entries>
<entry name='"java:/ConnectionFactory"/'>
</entry></entries>
</connection-factory>
<connection-factory name='"RemoteConnectionFactory"'>
<connectors>
<connector-ref connector-name='"http-connector"/'>
</connector-ref></connectors>
<entries>
<entry name='"java:jboss/exported/jms/RemoteConnectionFactory"/'>
</entry></entries>
</connection-factory>
<pooled-connection-factory name='"hornetq-ra"'>
<transaction mode='"xa"/'>
<connectors>
<connector-ref connector-name='"in-vm"/'>
</connector-ref></connectors>
<entries>
<entry name='"java:/JmsXA"/'>
<!-- Global JNDI entry used to provide a default JMS Connection factory to EE application -->
<entry name='"java:jboss/DefaultJMSConnectionFactory"/'>
</entry></entry></entries>
</transaction></pooled-connection-factory>
</jms-connection-factories>
<jms-destinations>
<jms-queue name='"ExpiryQueue"'>
<entry name='"java:/jms/queue/ExpiryQueue"/'>
</entry></jms-queue>
<jms-queue name='"DLQ"'>
<entry name='"java:/jms/queue/DLQ"/'>
</entry></jms-queue>
<jms-queue name='"TestQueue"'>
<entry name='"java:jboss/jms/queue/testQueue"/'>
<durable>false</durable>
</entry></jms-queue>
</jms-destinations>
</hornetq-server>
</subsystem>

ass you see here we have jms-queue name="TestQueue"

JEE App

xml hell .... pom.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<project http:="" maven-4.0.0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsd="" xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<modelversion>4.0.0</modelversion>
<groupid>pl.btbw</groupid>
<artifactid>JMS_example_4</artifactid>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupid>javax</groupid>
<artifactid>javaee-api</artifactid>
<version>7.0</version>
</dependency>
<dependency>
<groupid>org.jboss.spec.javax.jms</groupid>
<artifactid>jboss-jms-api_2.0_spec</artifactid>
<version>1.0.0.Alpha1</version>
</dependency>
</dependencies>
<build>
<finalname>JMS_example_4</finalname>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-compiler-plugin</artifactid>
<configuration>
<source/>1.8
                    <target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactid>maven-war-plugin</artifactid>
<configuration>
<failonmissingwebxml>false</failonmissingwebxml>
</configuration>
</plugin>
</plugins>
</build>
</project>

ok time for java

App.java JAX-RS configuration

package pl.btbw.core;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
public class App extends Application {
}

MyCtrl.java

package pl.btbw.web;

import javax.inject.Inject;
import javax.ws.rs.PathParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/")
public class MyCtrl {

    @Inject
    private JMSSender jmsSender;

    @GET
    @Path("/send/{message}")
    public String sendMessage(@PathParam("message") String message) {
        jmsSender.sendMessage(message);
        return "sent message: " + message;
    }

}

JMSSender.java

package pl.btbw.web;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.jms.*;

@Stateless
public class JMSSender {

    @Resource(mappedName = "java:jboss/jms/queue/testQueue")
    private Queue queueExample;

    @Inject
    private JMSContext context;

    public void sendMessage(String txt) {
        try {
            context
                    .createProducer()
                    .send(queueExample, txt);
        }
        catch (Exception exc) {
            exc.printStackTrace();
        }
    }
}

AsyncReceiver.java

package pl.btbw.web;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(name = "AsyncReceiver", activationConfig = {
        @ActivationConfigProperty(
                propertyName = "destinationLookup",
                propertyValue = "java:jboss/jms/queue/testQueue"),
        @ActivationConfigProperty(
                propertyName = "destinationType",
                propertyValue = "javax.jms.Queue")
})
public class AsyncReceiver implements MessageListener {

    @Override
    public void onMessage(Message message) {
        try {
            String text = ((TextMessage) message).getText();
            System.out.println("Message form Queue: " + text);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}