Proper vertx versions for push notifications - java

I tried to run next code, but for 2.1.6 vertex library occurs io.vertx package not found problem.
Or if somebody have a small project with Java and JS code for this, give please a link.
Also I tried 3.x.x version, but it has no io.vertx method.
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.http.HttpServer;
import org.vertx.java.core.sockjs.SockJSServer;
import org.vertx.java.core.sockjs.impl.DefaultSockJSServer;
//...
Vertx vertx = Vertx.newVertx();
EventBus eventBus = vertx.eventBus()
HttpServer server = vertx.createHttpServer();
JsonArray permitted = new JsonArray();
permitted.add(new JsonObject());
SockJSServer sockJSServer = new DefaultSockJSServer(vertx, server);
sockJSServer.bridge(new JsonObject().putString("prefix", "/pusher"), permitted, permitted);
server.listen(<some port>);

Vert.x versions 2.x used the org.vertx package, whereas the 3.x branch uses io.vertx, this is why you're running into problems.
For version 2 there are some examples regarding the event bus bridge:
https://github.com/vert-x/vertx-examples/tree/master/src/raw/java/eventbusbridge
And a lot of examples for Vert.x 3:
https://github.com/vert-x3/vertx-examples
So basically you should just stick to one of the versions and you should be fine.

Related

Which region endpoint should AWS Java SDK v2 be using for Route 53?

On Windows 10 I'm using the AWS Java SDK v2 (software.amazon.awssdk:route53:2.8.3) and I'm trying to merely connect and list all my Route 53 hosted zones. I have us-west-1 specified in my user configuration (in my .aws/config file) as the default region.
I create a Route53Client using the following:
Route53Client route53Client = Route53Client.builder().build();
Note that I don't indicate a region, because in the online documentation it says:
When you submit requests using the AWS CLI or SDKs, either leave the Region and endpoint unspecified, or specify us-east-1 as the Region.
I then try to list hosted zones using something like this:
Set<HostedZone> hostedZones = client.listHostedZonesPaginator().stream()
.flatMap(response -> response.hostedZones().stream())
.collect(Collectors.toSet());
In the logs I see a debug message like this:
[DEBUG] Unable to load region from software.amazon.awssdk.regions.providers.SystemSettingsRegionProvider#...:Unable to load region from system settings. Region must be specified either via environment variable (AWS_REGION) or system property (aws.region).
Then it throws a java.net.UnknownHostException for route53.us-west-1.amazonaws.com.
Granted I am on a spotty Internet connection right now. Is that the correct endpoint? If it is, the why isn't that endpoint listed at https://docs.aws.amazon.com/general/latest/gr/rande.html ? If it isn't, why is it trying to connect to a us-west1 endpoint, if I'm following the online documentation (as I quoted above), which indicates that a region need not be indicated? Or is the problem simply my Internet connection and spotty DNS lookup at the moment?
The AWS SDK development team decided to require Route53 requests to explicitly indicate the Region.AWS_GLOBAL or requests would not work, as someone noted in Issue #456 for the SDK:
To access Route53 you would currently need to specify the AWS_GLOBAL region. This was done to prevent customers from using global services and not realizing that for this service your calls are likely not staying in region and could potentially be spanning the globe.
Unfortunately Amazon didn't bother documenting this in the SDK (that I could find), and didn't provide a helpful error message, instead assuming developers would somehow guess the problem when the SDK tried to access an endpoint that did not exist even though the SDK was being used according to the API and according to the online documentation.
In short the Route53 client must be created like this:
route53Client = Route53Client.builder().region(Region.AWS_GLOBAL).build();
Here is the AWS Route 53 V2 Code example that lists hosted zones:
package com.example.route;
//snippet-start:[route.java2.list_zones.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.route53.Route53Client;
import software.amazon.awssdk.services.route53.model.HostedZone;
import software.amazon.awssdk.services.route53.model.Route53Exception;
import software.amazon.awssdk.services.route53.model.ListHostedZonesResponse;
import java.util.List;
//snippet-end:[route.java2.list_zones.import]
public class ListHostedZones {
public static void main(String[] args) {
Region region = Region.AWS_GLOBAL;
Route53Client route53Client = Route53Client.builder()
.region(region)
.build();
listZones(route53Client);
}
//snippet-start:[route.java2.list_zones.main]
public static void listZones(Route53Client route53Client) {
try {
ListHostedZonesResponse zonesResponse = route53Client.listHostedZones();
List<HostedZone> checklist = zonesResponse.hostedZones();
for (HostedZone check: checklist) {
System.out.println("The name is : "+check.name());
}
} catch (Route53Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
//snippet-end:[route.java2.list_zones.main]
}

How to add HiveMQ Client as a dependency to HiveMQ Community Edition using Gradle?

I'd like to combine HiveMQ Client and HiveMQ Community Edition which is the implementation for the broker into one project. I tried adding the HiveMQ client as a dependency to the build.gradle file in the Hive MQ Community Edition (broker). It was able to build successfully but I'm not sure if I did it correctly. When I tried to reference client classes inside the Community Edition it gives me errors. Am I missing something? I want to be able to just put put the client project inside of the broker community edition and be able to create a client and access all of the classes I could in HiveMQ client. I left the instructions from the HiveMQ Client website, links, and also what the build.gradle file looks like the HiveMQ community edition.
Error I'm getting: The import com.hivemq.client cannot be resolved (Happens to all imports referencing anything in the HiveMQ Client project)
Link to the HiveMQ GitHubs:
https://github.com/hivemq/hivemq-mqtt-client
https://github.com/hivemq/hivemq-community-edition
Code from Main.Java that produces the error
package com.main;
import java.util.UUID;
import com.hivemq.client.mqtt.MqttGlobalPublishFilter;
import com.hivemq.client.mqtt.datatypes.MqttQos;
import com.hivemq.client.mqtt.mqtt5.Mqtt5BlockingClient;
import com.hivemq.client.mqtt.mqtt5.Mqtt5BlockingClient.Mqtt5Publishes;
import com.hivemq.client.mqtt.mqtt5.Mqtt5Client;
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5Publish;
import java.util.logging.Logger;
import java.nio.ByteBuffer;
import java.util.Optional;
import java.util.logging.Level;
import java.util.concurrent.TimeUnit;
public class Main {
private static final Logger LOGGER = Logger.getLogger(Main.class.getName()); // Creates a logger instance
public static void main(String[] args) {
// Creates the client object using Blocking API
Mqtt5BlockingClient client1 = Mqtt5Client.builder()
.identifier(UUID.randomUUID().toString()) // the unique identifier of the MQTT client. The ID is randomly generated between
.serverHost("0.0.0.0") // the host name or IP address of the MQTT server. Kept it 0.0.0.0 for testing. localhost is default if not specified.
.serverPort(1883) // specifies the port of the server
.buildBlocking(); // creates the client builder
client1.connect(); // connects the client
System.out.println("Client1 Connected");
String testmessage = "How is it going";
byte[] messagebytesend = testmessage.getBytes(); // stores a message as a byte array to be used in the payload
try {
Mqtt5Publishes publishes = client1.publishes(MqttGlobalPublishFilter.ALL); // creates a "publishes" instance thats used to queue incoming messages
client1.subscribeWith() // creates a subscription
.topicFilter("test/topic")
.send();
System.out.println("The client has subscribed");
client1.publishWith() // publishes the message to the subscribed topic
.topic("test/topic")
.payload(messagebytesend)
.send();
Mqtt5Publish receivedMessage = publishes.receive(5,TimeUnit.SECONDS).orElseThrow(() -> new RuntimeException("No message received.")); // receives the message using the "publishes" instance
LOGGER.info("Recieved: " + receivedMessage);
byte[] getdata = receivedMessage.getPayloadAsBytes();
System.out.println(getdata.toString());
System.out.println(receivedMessage);
}
catch (Exception e) { // Catches all exceptions using the "base exception"
LOGGER.log(Level.SEVERE, "Something went wrong.", e);
}
}
}
I didn't have the HiveMQ client in my build path. On the line with red errors Eclipse gave me the option of fixing the project set up and I click on it and it automatically added the HiveMQ client to the build path. I posted a screenshot below.

Unable to create grpc connection in 32bit windows machine

I am trying to access the list of Subscribers in my GCP Project using my java code and few libraries provided by GCP. This code works fine in my 64 bit Windows environment but not working in 32bit windows environment.
I have seen in a few documents, saying that netty is not supported on 32bit machines and we can build our own binaries if required.
https://netty.io/wiki/forked-tomcat-native.html#how-to-build
CredentialsProvider credentialsProvider =
FixedCredentialsProvider.create(
ServiceAccountCredentials.fromStream(new FileInputStream(JSONPath)));
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create(SubscriptionAdminSettings.newBuilder().setCredentialsProvider(credentialsProvider).build())) {
ListSubscriptionsRequest listSubscriptionsRequest =
ListSubscriptionsRequest.newBuilder()
.setProject(ProjectName.of(ProjectId).toString())
.build();
SubscriptionAdminClient.ListSubscriptionsPagedResponse response =
subscriptionAdminClient.listSubscriptions(listSubscriptionsRequest);
logger.log(Level.SEVERE,"response List: "+response.toString());
Iterable<Subscription> subscriptions = response.iterateAll();
for (Subscription subscription : subscriptions) {
if(subscription.getName().equals(SubscriptionId)){
return true;
}
}
[20:02:30:384]|[06-17-2019]|[io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts]|[INFO]|[36]: netty-tcnative unavailable (this may be normal)|
java.lang.IllegalArgumentException: Failed to load any of the given libraries: [netty_tcnative_windows_x86_32, netty_tcnative_x86_32, netty_tcnative] at io.grpc.netty.shaded.io.netty.util.internal.NativeLibraryLoader.loadFirstAvailable(NativeLibraryLoader.java:104)
at io.grpc.netty.shaded.io.netty.handler.ssl.OpenSsl.loadTcNative(OpenSsl.java:526)
at io.grpc.netty.shaded.io.netty.handler.ssl.OpenSsl.(OpenSsl.java:93)
at io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts.defaultSslProvider(GrpcSslContexts.java:244)
at io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts.configure(GrpcSslContexts.java:171)
at io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts.forClient(GrpcSslContexts.java:120)
at io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder.buildTransportFactory(NettyChannelBuilder.java:385)
at io.grpc.internal.AbstractManagedChannelImplBuilder.build(AbstractManagedChannelImplBuilder.java:435)
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:254)
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:165)
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:157)
at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:157)
at com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub.create(GrpcSubscriberStub.java:260)
at com.google.cloud.pubsub.v1.stub.SubscriberStubSettings.createStub(SubscriberStubSettings.java:241)
at com.google.cloud.pubsub.v1.SubscriptionAdminClient.(SubscriptionAdminClient.java:177)
at com.google.cloud.pubsub.v1.SubscriptionAdminClient.create(SubscriptionAdminClient.java:158)
The grpc-java SECURITY.md describes your options:
Use Java 9+, which support ALPN without need for tcnative
For 32 bit Windows specifically, you can Conscrypt
The documentation also describes how to use Conscrypt. Namely to add a dependency on conscrypt-openjdk-uber and to register it as the default security provider:
import org.conscrypt.Conscrypt;
import java.security.Security;
...
// Somewhere in main()
Security.insertProviderAt(Conscrypt.newProvider(), 1);
You answered your own question... netty-tcnative-* does not support 32bit platforms so you will need to compile it yourself and include it in your class-path.

JCSMP Solace Connection

I am working on creating a Solace Connection. I am using JCSMP package within Solace for that
import com.solacesystems.jcsmp.JCSMPException;
import com.solacesystems.jcsmp.JCSMPFactory;
import com.solacesystems.jcsmp.JCSMPProperties;
import com.solacesystems.jcsmp.JCSMPSession;
import com.solacesystems.jcsmp.JCSMPStreamingPublishEventHandler;
import com.solacesystems.jcsmp.TextMessage;
import com.solacesystems.jcsmp.Topic;
import com.solacesystems.jcsmp.XMLMessageProducer;
in ways like:
final JCSMPProperties properties = new JCSMPProperties();
So i am trying to read up on it to get a better idea. But I cannot find any documentation online. And I have spent like 2 days searching for it before posting here.
Can anyone guide me for the same.
Thanks
I realize this answer is extremely after the fact, but Solace now has their API documentation available online here:
http://dev.solacesystems.com/docs/enterprise-api-docs/
Note that I currently work for Solace but provide the link because I believe it is what is being requested in the question.

spring social connectivity with Facebook and accessing user data

I have started to work with spring social and following the tutorial from here. and pages that follow.
My java file looks like this.
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
//import org.springframework.boot.SpringApplication;
//import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.support.ConnectionFactoryRegistry;
import org.springframework.social.facebook.api.Comment;
import org.springframework.social.facebook.api.CommentOperations;
import org.springframework.social.facebook.connect.FacebookConnectionFactory;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.api.impl.FacebookTemplate;
import org.springframework.social.oauth2.AccessGrant;
import org.springframework.social.oauth2.GrantType;
import org.springframework.social.oauth2.OAuth2Operations;
import org.springframework.social.oauth2.OAuth2Parameters;
//import org.springframework.social.UserIdSource;
//import org.springframework.social.connect.ConnectionFactoryLocator;
//import org.springframework.social.connect.ConnectionRepository;
//import org.springframework.social.connect.web.ConnectController;
#Configuration
#EnableAutoConfiguration
#Import(FacebookConfig.class)
#ComponentScan
public class App {
static private String accessToken = "accesstoken";
static private String secretKey = "secretkey";
static private String clientId = "clientid";
public static void main(String[] args) {
FacebookConnectionFactory connectionFactory = new FacebookConnectionFactory(clientId, secretKey);
OAuth2Operations oauthOperations = connectionFactory.getOAuthOperations();
OAuth2Parameters params = new OAuth2Parameters();
params.setRedirectUri("http://facebook.com");
String authorizeUrl = oauthOperations.buildAuthorizeUrl(GrantType.IMPLICIT_GRANT, params);
AccessGrant accessGrant = new AccessGrant(accessToken);
System.out.println(accessGrant.getAccessToken());
System.out.println(accessGrant.getExpireTime());
System.out.println(accessGrant.getScope());
ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
registry.addConnectionFactory(connectionFactory);
Facebook facebook = new FacebookTemplate(accessToken);
}
}
When i run this code i get the error as stated.
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2:exec (default-cli) on project mavenproject3: Command execution failed. Process exited with an error: 1(Exit value: 1) -> [Help 1]
To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.
For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
When i comment out this line:
Facebook facebook = new FacebookTemplate(accessToken);
It works fine.
Can someone suggest me the requisite. I am a newbie so please bear with me.
The URL you mentioned isn't really a tutorial as much as it is a reference. Admittedly, it gets a bit detailed and isn't very helpful for the new user. Duly noted...expect a new tutorial to be written as soon as I get a moment to do so.
Where did you get the value of accessToken? If you didn't get it via an OAuth2 "dance" with Facebook, then it's not going to work.
First, I see you creating a FacebookConnectionFactory to obtain an OAuth2Operations, through which you set a redirect URI, etc, etc..and then build an authorization URL for IMPLICIT grant. There are several things out of sorts there:
Facebook doesn't support IMPLICIT grant. It only supports authorization code grant and client token grant. Even so, with implicit grant and authorization code grant your app must redirect to Facebook (in a web browser) to obtain permission from the user. Once that's granted, then it will redirect back to your app...speaking of which...
The redirect URI you set is http://facebook.com. That should be the URL of your application where Facebook will redirect back to after authorization.
After all of that, you never even use the authorizeUrl...it's just in a String. It wouldn't work even if you did use it, for the reasons already mentioned, but the first 5 or so lines are all for nothing.
You create a ConnectionFactoryRegistry and register the FacebookConnectionFactory with it...but then you do nothing with the ConnectionFactoryRegistry. That's okay...you almost never need to do anything with it anyway, because it primarily exists as a helper to ConnectController.
There's simply no good way of obtaining a user-oriented access token without the redirect "dance". It's important to get permission from the user you'll be accessing Facebook on behalf of. If it were any easier than that, it'd be way too easy to create an app that spams Facebook and essentially ruins the experience for everyone.
The work of obtaining an access token via that redirect "dance" is handled automatically by the framework using ConnectController. Sure, you can do it yourself if you'd rather, but ConnectController will handle all of that for you.
For lack of a proper tutorial at the moment, I recommend that you have a look at https://github.com/spring-projects/spring-social-samples/tree/master/spring-social-showcase. Also, there's a Spring Boot-oriented version of it at https://github.com/spring-projects/spring-social-samples/tree/master/spring-social-showcase-boot that simplifies the configuration more (albeit, it relies on changes that aren't in an official Spring Boot release yet).

Categories

Resources