# Write (send) a message into a WildFly Topic

In 
JMS
Published 2022-12-03

This tutorial explains to you how we can write (send) a simple message to a WildFly topic using Java. I used a WildFly 10 Application Server for this tutorial.

In my example, I will use jmsuser to publish a message to an WildFly Topic. In order to use this user you have to create a WildFly user and give him the proper rights. When you create the user using the add-user.bat (on Windows) utility you have to give him the guest role. The guest role is defined in standalone-full.xml configuration file:

<security-setting name="#">
   <role name="guest" delete-non-durable-queue="true"
       create-non-durable-queue="true"
       delete-durable-queue="true"
       create-durable-queue="true"
       consume="true"
       send="true" />
</security-setting>

Also, you have to verify that application-roles.properties file has the following line:

jmsuser=guest

Here is the code I put on my JSF (.xhtml) page to create a button to call a Java class to publish a message to the WildFly Topic:

<h:form> 
    <p:commandbutton value="Send Message to Topic" id="ajax" actionlistener="#{wildflyJms.sendMessage}">
    </p:commandbutton>
</h:form>

Here is the WildflyJms.java file I used:

/*
 *   Here you have an example of Java code for publishing a message to a WildFly Topic
 *
 */

package resources;

import java.util.Hashtable;
import java.util.Properties;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import javax.naming.*;
import javax.jms.*;

@ManagedBean (name="wildflyJms")
public class WildflyJms {

  // Set the JNDI context factory for a WildFly Server.
  public final static String JNDI_FACTORY="org.jboss.ejb.client.naming";

  // Set the JMS context factory.
  public final static String JMS_FACTORY="java:jboss/exported/jms/RemoteConnectionFactory";

  // Set the topic.
  public final static String MY_TOPIC="java:/MyTopic";

  // Set Wildfly URL.
  public final static String WildflyURL="http-remoting://localhost:8080";
  
  public void sendMessage(String message) throws Exception {

    //1) Create and start a connection 
    Properties properties = new Properties();
    properties.put(Context.URL_PKG_PREFIXES, JNDI_FACTORY);

    InitialContext ic=new InitialContext(properties);

    TopicConnectionFactory f=(TopicConnectionFactory)ic.lookup(JMS_FACTORY) ;

    TopicConnection con=f.createTopicConnection("jmsuser","jmsuser");
    con.start();

    //2) create topic session  
    TopicSession ses=con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

    //3) get the Topic object  
    Topic t=(Topic)ic.lookup(MY_TOPIC);

    //4)create TopicPublisher object         
    TopicPublisher topicPublisher = ses.createPublisher(null);

    //5) create TextMessage object  
    TextMessage msg=ses.createTextMessage();
    msg.setText("Hello");

    //6) send/ publish a message  
    topicPublisher.publish(t, msg);
    System.out.println("Message successfully sent to a WildFly Topic.");

    //7) connection close 
    con.close();
  }
}