# Send a message using SAF (WebLogic)

In 
JMS
Published 2022-12-03

This tutorial shows you how you can send a message to a remote Queue using local SAF services (WebLogic) with Java.

# SAF services (WebLogic)

JMS messages can fail to be delivered for a number of reasons, such as a network or server failure, a quota failure on a receiving server or a security denial. WebLogic Server offers the SAF service for reliably delivering messages between distributed applications running on different WebLogic Server remote instances. The reliability is provided through a mechanism whereby the local server saves any messages it can't successfully send to remote JMS queues or topics. Instead of discarding the messages, WebLogic Server saves the messages on the local server instance and redelivers them automatically to the remote server when it becomes available again.

Writing code for sending messages to a Remote Queue/ Topic using SAF services is similar to sending messages to a Local Queue/ Topic.

Send a message using SAF services with Java

Here is the SendMessageSaf.java file used to send an email to a Remote Queue using SAF services:

//
//   Send a message using SAF services with Java
//
package com;

import java.util.Hashtable;
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
public class SendMessageSaf {

  // Defines the JNDI context factory.
  public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

  // Defines the JMS context factory.
  public final static String JMS_FACTORY="myConnectionFactory";

  // Defines the queue.
  public final static String QUEUE="mySAF_myLocalQueue";

  // The port is for the Managed Server or the
  //  Admin Server where the JMS Server is running.
  public final static String WebLogicURL="t3://pc:7004";
  
  public void sendMessage(String message) throws Exception {
    System.out.println(message);
    
    //1. Create and start connection 
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
    env.put(Context.PROVIDER_URL, WebLogicURL);
    
    InitialContext ic=new InitialContext(env);

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

    QueueConnection con=f.createQueueConnection();
    con.start();

    //2. Create queue session  
    QueueSession ses=con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

    //3. Get the Queue object  
    Queue t=(Queue)ic.lookup(QUEUE);

    //4. Create QueueSender object         
    QueueSender sender=ses.createSender(t);

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

    //6. send message  
    sender.send(msg);
    System.out.println("Message successfully sent.");

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

Take a look at this picture:

You can see that the Java code is almost the same as in the case we are not using SAF services. The difference is the name of the Queue (JNDI name) we are writing to. This name is the JNDI Prefix of the SAF Imported Destination concatenated with the Local JNDI Name of the Queue (SAFQueue) we have created in a SAF imported Destinations.