Commands | AMQP1 Sessions

In the context of the AMQP (Advanced Message Queuing Protocol) 1.0.0 specification, a session represents a logical context for communication between a client and a message broker. Here's a breakdown of what an AMQP 1.0.0 session entails:

 

 

In summary, an AMQP 1.0.0 session provides a logical context for message exchange between an AMQP client and broker, facilitating transactional integrity, flow control, and resource management within the messaging system. It defines the boundaries within which messaging operations are performed and helps ensure the efficient and reliable exchange of messages.

 

 

Open Session

The method CreateSession creates a new session with the given name (or if empty, it creates with a random name), if the session already exists an exception is raised. The client allows you to create multiple session using the same AMQP connection.

 

Once the session is successfully created, the event OnAQMPSessionOpen is fired.

 

oAMQP.CreateSession('MySession');
procedure OnAMQPSessionOpen(Sender: TObject; const aSession: TsgcAMQP1Session; const aBegin: TsgcAMQP1FrameBegin);
begin
  ShowMessage('#session-open: ' + aSession.Id);
end;

 

The CreateSession method returns the TsgcAMQP1Session class which contains the session information.

 

 

Await Open Session

 

By default, the CreateSession method is Asynchronous, so after calling the method, the code continue. If you want to wait till the CreateSession method is completed and the confirmation sent by the server is received, set the property Await to True in the Options parameter.

 


procedure OpenSession(const aSession: string);
var
  oOptions: TsgcAMQP1MethodOptions_SessionOpen;
begin
  oOptions := TsgcAMQP1MethodOptions_SessionOpen.Create;
  Try
    oOptions.Await := True;
    AMQP1.CreateSession(aSession, oOptions);
  Finally
    oOptions.Free;
  End;
end;

 

Close Session

To Close an existing session use the method CloseSession passing the name of the session to close.

 

Once the session is successfully closed, the event OnAQMPSessionClose is fired.


oAMQP.CloseSession('MySession');
procedure OnAMQPSessionCloseEvent(Sender: TObject; const aSession: TsgcAMQP1Session; const aEnd: TsgcAMQP1FrameEnd);
begin
  ShowMessage('#session-close: ' + aSession.Id + ' [' + IntToStr(aSession.Channel) + ']' + ' reason: ' + aEnd.Error.Condition);
end;

 

Await Close Session

 

By default, the CloseSession method is Asynchronous, so after calling the method, the code continue. If you want to wait till the CloseSession method is completed and the confirmation sent by the server is received, set the property Await to True in the Options parameter.

 


procedure CloseSession(const aSession: string);
var
  oOptions: TsgcAMQP1MethodOptions_SessionClose;
begin
  oOptions := TsgcAMQP1MethodOptions_SessionClose.Create;
  Try
    oOptions.Await := True;
    AMQP1.CloseSession(aSession, oOptions);
  Finally
    oOptions.Free;
  End;
end;