Skip to content

Commit

Permalink
Merge pull request #84 from ayeshLK/local_development
Browse files Browse the repository at this point in the history
Remove Ballerina `handle` usage in client classes
  • Loading branch information
shafreenAnfar committed Aug 3, 2023
2 parents caaa1bd + fe05b17 commit 754ca7e
Show file tree
Hide file tree
Showing 29 changed files with 1,082 additions and 1,516 deletions.
42 changes: 1 addition & 41 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@
dependencies-toml-version = "2"
distribution-version = "2201.6.0"

[[package]]
org = "ballerina"
name = "io"
version = "1.4.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.value"}
]

[[package]]
org = "ballerina"
name = "jballerina.java"
Expand All @@ -24,43 +15,12 @@ modules = [
{org = "ballerina", packageName = "jballerina.java", moduleName = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.value"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "log"
version = "2.7.1"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.value"},
{org = "ballerina", name = "observe"}
]
modules = [
{org = "ballerina", packageName = "log", moduleName = "log"}
]

[[package]]
org = "ballerina"
name = "observe"
version = "1.0.7"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerinax"
name = "java.jms"
version = "0.1.3"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "log"}
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerinax", packageName = "java.jms", moduleName = "java.jms"}
Expand Down
8 changes: 5 additions & 3 deletions ballerina/caller.bal
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
// specific language governing permissions and limitations
// under the License.

import ballerina/jballerina.java;

# Represents a JMS caller, which can be used to commit the offsets consumed by the service.
public isolated client class Caller {

# Mark a JMS message as received.
#
# + message - JMS message record
# + return - `jms:Error` if there is an error in the execution or else nil
isolated remote function acknowledge(Message message) returns Error? {
return externConsumerAcknowledge(message);
}
isolated remote function acknowledge(Message message) returns Error? = @java:Method {
'class: "io.ballerina.stdlib.java.jms.JmsConsumer"
} external;
}
86 changes: 36 additions & 50 deletions ballerina/connection.bal
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,59 @@
// under the License.

import ballerina/jballerina.java;
import ballerina/log;

# Represents JMS Connection.
#
# + config - Used to store configurations related to a JMS Connection
public isolated client class Connection {
public final readonly & ConnectionConfiguration config;
private final handle jmsConnection;
private final readonly & ConnectionConfiguration config;

# JMS Connection constructor
public isolated function init(*ConnectionConfiguration connectionConfig) returns error? {
# Initialize and starts a JMS connection.
#
# + connectionConfig - The configurations to be used when initializing the JMS connection
# + return - The `jms:Connection` or an `jms:Error` if the initialization failed
public isolated function init(*ConnectionConfiguration connectionConfig) returns Error? {
self.config = connectionConfig.cloneReadOnly();
string icf = self.config.initialContextFactory;
string providerUrl = self.config.providerUrl;
string factoryName = self.config.connectionFactoryName;
self.jmsConnection = check createJmsConnection(
icf, providerUrl, factoryName, connectionConfig.properties);
return self.externInit(connectionConfig);
}

# Create a Session object, specifying transacted and acknowledgeMode
isolated function externInit(ConnectionConfiguration connectionConfig) returns Error? = @java:Method {
name: "init",
'class: "io.ballerina.stdlib.java.jms.JmsConnection"
} external;

# Create a Session object, specifying transacted and acknowledgeMode.
#
# + acknowledgementMode - Configuration indicating how messages received by the session will be acknowledged
# + ackMode - Configuration indicating how messages received by the session will be acknowledged
# + return - Returns the Session or an error if it fails.
isolated remote function createSession(AcknowledgementMode acknowledgementMode = AUTO_ACKNOWLEDGE) returns Session|error {
return new Session(self.jmsConnection, acknowledgementMode);
isolated remote function createSession(AcknowledgementMode ackMode = AUTO_ACKNOWLEDGE) returns Session|error {
return new Session(self, ackMode);
}

# Starts (or restarts) a connection's delivery of incoming messages.
# A call to start on a connection that has already been started is ignored.
isolated remote function 'start() {
error? err = startJmsConnection(self.jmsConnection);
if (err is error) {
log:printError("Error starting connection", err);
}

}
#
# + return - A `jms:Error` if threre is an error while starting the connection
isolated remote function 'start() returns Error? = @java:Method {
name: "start",
'class: "io.ballerina.stdlib.java.jms.JmsConnection"
} external;

# Temporarily stops a connection's delivery of incoming messages.
# Delivery can be restarted using the connection's start method.
isolated remote function stop() {
error? err = stopJmsConnection(self.jmsConnection);
if (err is error) {
log:printError("Error stopping connection", err);
}
}
#
# + return - A `jms:Error` if threre is an error while stopping the connection
isolated remote function stop() returns Error? = @java:Method {
'class: "io.ballerina.stdlib.java.jms.JmsConnection"
} external;

isolated function getJmsConnection() returns handle {
return self.jmsConnection;
}
# Closes the connection.
#
# + return - A `jms:Error` if threre is an error while closing the connection
isolated remote function close() returns Error? = @java:Method {
'class: "io.ballerina.stdlib.java.jms.JmsConnection"
} external;
}

# Configurations related to a JMS connection
# Configurations related to a JMS connection.
#
# + initialContextFactory - JMS provider specific inital context factory
# + providerUrl - JMS provider specific provider URL used to configure a connection
Expand All @@ -78,22 +79,7 @@ public type ConnectionConfiguration record {|
string initialContextFactory = "wso2mbInitialContextFactory";
string providerUrl = "amqp://admin:admin@ballerina/default?brokerlist='tcp://localhost:5672'";
string connectionFactoryName = "ConnectionFactory";
string? username = ();
string? password = ();
string username?;
string password?;
map<string> properties = {};
|};

isolated function createJmsConnection(string initialContextFactory, string providerUrl,
string connectionFactoryName, map<string> otherPropeties) returns handle|error = @java:Method {
'class: "io.ballerina.stdlib.java.jms.JmsConnectionUtils"
} external;

isolated function startJmsConnection(handle jmsConnection) returns error? = @java:Method {
name: "start",
'class: "javax.jms.Connection"
} external;

isolated function stopJmsConnection(handle jmsConnection) returns error? = @java:Method {
name: "stop",
'class: "javax.jms.Connection"
} external;
47 changes: 18 additions & 29 deletions ballerina/destination.bal
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,23 @@
// specific language governing permissions and limitations
// under the License.

import ballerina/jballerina.java;
# Represent the JMS destination.
#
# + 'type - JMS destination types
# + name - Name of the destination
public type Destination readonly & record {|
DestinationType 'type;
string name?;
|};

# Represent the JMS destination
public type Destination distinct object {

isolated function getJmsDestination() returns handle;
};

function getDestination(handle jmsDestination) returns Destination|error {
handle jmsDestinationType = getDestinationType(jmsDestination);
string? destinationType = java:toString(jmsDestinationType);
match destinationType {
"queue" => {
return new Queue(jmsDestination);
}
"topic" => {
return new Topic(jmsDestination);
}
"temporaryQueue" => {
return new TemporaryQueue(jmsDestination);
}
"temporaryTopic" => {
return new TemporaryTopic(jmsDestination);
}
}
return error Error("Invalid destination type");
# Defines the supported JMS destination types.
public enum DestinationType {
# Represents JMS Queue
QUEUE = "QUEUE",
# Represents JMS Temporary Queue
TEMPORARY_QUEUE = "TEMPORARY_QUEUE",
# Represents JMS Topic
TOPIC = "TOPIC",
# Represents JMS Temporary Topic
TEMPORARY_TOPIC = "TEMPORARY_TOPIC"
}

function getDestinationType(handle destination) returns handle = @java:Method {
'class: "io.ballerina.stdlib.java.jms.JmsDestinationUtils"
} external;
19 changes: 0 additions & 19 deletions ballerina/jms_commons.bal

This file was deleted.

17 changes: 8 additions & 9 deletions ballerina/message.bal
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,8 @@ public type Message record {
string messageId?;
int timestamp?;
string correlationId?;
record {|
JmsDestinationType 'type;
string name?;
|} replyTo?;
record {|
JmsDestinationType 'type;
string name?;
|} destination?;
Destination replyTo?;
Destination destination?;
int deliveryMode?;
boolean redelivered?;
string jmsType?;
Expand Down Expand Up @@ -75,9 +69,14 @@ public type BytesMessage record {|
byte[] content;
|};

isolated function externWriteText(handle message, handle value) returns error? = @java:Method {
name: "setText",
'class: "javax.jms.TextMessage"
} external;

isolated function externWriteBytes(handle message, byte[] value) returns error? = @java:Method {
name: "writeBytes",
'class: "io.ballerina.stdlib.java.jms.JmsBytesMessageUtils"
'class: "io.ballerina.stdlib.java.jms.JmsBytesMessage"
} external;

isolated function externSetBoolean(handle message, handle name, boolean value) returns error? = @java:Method {
Expand Down
Loading

0 comments on commit 754ca7e

Please sign in to comment.