Commit 50e8eb3b authored by rawan's avatar rawan

Initial commit

parents
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>protobuf_example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- مكتبة Protobuf -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>4.28.2</version>
</dependency>
<!-- مكتبة مساعدة لحل مشاكل Accessors -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.25.3</version>
</dependency>
<!-- مكتبات gRPC -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.63.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.63.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.63.0</version>
</dependency>
<!-- javax.annotation لدعم Java 17+ -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.7.0</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.25.3:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.63.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package org.example;
import com.example.book.Book;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.Arrays;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) {
// Nested Message
Book.Publisher publisher = Book.Publisher.newBuilder()
.setName("O'Reilly")
.setLocation("USA")
.build();
// Book
Book myBook = Book.newBuilder()
.setTitle("gRPC")
.setAuthor("Rawan")
.setIsbn("123 456 789")
.setPublicationYear(2025)
.setGenre(Book.Genre.SCIENCE)
.setPublisher(publisher)
.build();
System.out.println(" before encoding ");
System.out.println(myBook);
// Serialization
byte[] bookAsBytes = myBook.toByteArray();
System.out.println("\n Encode ");
System.out.println("Coded to " + bookAsBytes.length + " bytes");
System.out.println(" "+ Arrays.toString(bookAsBytes));
// Deserialization
try {
Book decodedBook = Book.parseFrom(bookAsBytes);
System.out.println("\n after decode");
System.out.println(decodedBook);
System.out.println("\n Data verification ");
System.out.println(" " + myBook.equals(decodedBook));
System.out.println("Is the original object equal to the decoded object : " + decodedBook.getPublisher().getName());
} catch (InvalidProtocolBufferException e) {
System.err.println("An error occurred while decoding : " + e.getMessage());
}
// Comparison with JSON
String bookAsJson = """
{
"title": "gRPC: The Definitive Guide",
"author": "Rawan",
"isbn": "123-456-789",
"publication_year": 2025,
"genre": "SCIENCE",
"publisher": {
"name": "O'Reilly",
"location": "USA"
}
}
""";
int jsonSize = bookAsJson.getBytes(StandardCharsets.UTF_8).length;
int protoSize = bookAsBytes.length;
System.out.println("\n Protobuf size comparison with JSON ");
System.out.println(" Protobuf: " + protoSize + " bytes");
System.out.println(" JSON: " + jsonSize + " bytes");
}
}
\ No newline at end of file
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.example.book";
service BookService {
// RPC function to fetch a book using its ISBN
rpc GetBook(GetBookRequest) returns (Book) {}
}
message GetBookRequest {
string isbn = 1;
}
message Book {
enum Genre {
GENRE_UNSPECIFIED = 0;
FICTION = 1;
NON_FICTION = 2;
SCIENCE = 3;
HISTORY = 4;
}
message Publisher {
string name = 1;
string location = 2;
}
string title = 1;
string author = 2;
string isbn = 3;
int32 publication_year = 4;
Genre genre = 5;
Publisher publisher = 6;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment